4

此代码适用于插入图像

但我想从谷歌表格中获取网址。我把 "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);
}

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

4

2 回答 2

7

这是一个可能的解决方案。当然,您必须用您自己的文档 ID 替换文档和电子表格 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());
}
于 2013-11-06T21:06:36.537 回答
3

This builds on Eduardo's Script (Thx Eduardo) and adds the feature of iteratively going through the spreadsheet and doing this process on every row. To do this I made it so that you can insert a different image in a different doc on each row.

Since AutoCrat doesn't support inserting images like this yet, this is the best work around I have found.

To test this script I put lastRow = 3. Change 3 to sheet.getLastRow() to do your full sheet.

Also note that "getsheetbyname" uses single quotes 'XXX' because it has a space in it, which breaks this otherwise.

function insertImage() {
    var startRow = 2;  // First row of data to process
    var lastRow = 3;   // Last row of data to process

    for (var i = startRow; i <= lastRow; i++)
    {
        // Get the Spreadsheet where the url is defined
        var sheet = SpreadsheetApp.openById("0AsrSazSXVGZtdEtQOTFpTTFzWFBhaGpDT3FWcVlIasd").getSheetByName('88. Research');


        // Get the target document.
        var docid = sheet.getRange("F"+i).getValue();

        var doc = DocumentApp.openById(docid);

        // Get the url from the correct cell
        var url = sheet.getRange("D"+i).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());
    }

}

// replace 3 with sheet.getLastRow()
于 2013-11-07T02:25:47.223 回答