将图片(来自网址)从Google表格单元格插入Google文档

Tim*_*nny 4 google-sheets google-apps-script

此代码适用于插入图像

但我想从谷歌表中获取网址.我把"UrlFetchApp.fetch("sourceSheet.getActiveCell().getValue()");"

在下面的代码中显示我对如何解决这个问题的思考...

  function insertImage() {



  // Retrieve an image from the web.
  var resp = UrlFetchApp.fetch("sourceSheet.getActiveCell().getValue()");

  // Create a document.
  var doc = DocumentApp.openById("1qWnGAR_WBpHkSdY5frld0VNfHtQ6BSzGxlzVNDi5xMk");

  // Append the image to the first paragraph.
  doc.getChild(2).asParagraph().appendInlineImage(resp);
}
Run Code Online (Sandbox Code Playgroud)

最终目标是我有一张表格,其中包含一列中文档的ID以及要插入另一列的图像的ulr.我希望脚本运行,以便我可以将每个图像插入每个文档.

Edu*_*rdo 7

这是一个可能的解决方案.当然,您必须使用自己的文档ID替换Document和Spreadsheet ID.

function insertImage() {
  // Get the target document.
  var doc = DocumentApp.openById("1DeAGfM1PXXXXXXXXXXXXXXXXXXwjjeUhhZTfpo");

  // Get the Spreadsheet where the url is defined
  var sheet = SpreadsheetApp.openById("0Agl8XXXXXXXXXXXXXXXXXXXXXXXXXRScWU2TlE");

  // Get the url from the correct celll
  var url = sheet.getRange("A1").getValue();

  // Retrieve an image from the web.
  var resp = UrlFetchApp.fetch(url);

  // Append the image to the first paragraph.
  doc.getChild(0).asParagraph().appendInlineImage(resp.getBlob());
}
Run Code Online (Sandbox Code Playgroud)