【问题标题】:Insert Image (from URL) from Google Sheet Cell to Google Doc将图像(来自 URL)从 Google 表格单元格插入到 Google Doc
【发布时间】:2013-11-06 17:11:43
【问题描述】:

此代码适用于插入图像

但我想从谷歌表格中获取网址。我把 "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。我希望脚本运行,以便我可以将每个图像插入到每个文档中。

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    这是一个可能的解决方案。当然,您必须将 Document 和 Spreadsheet 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());
    }
    

    【讨论】:

      【解决方案2】:

      这建立在Eduardo's Script (Thx Eduardo) 的基础上,并添加了迭代遍历电子表格并在每一行上执行此过程的功能。为此,我做了它,以便您可以在每一行的不同文档中插入不同的图像。

      由于 AutoCrat 还不支持插入这样的图像,这是我找到的最好的解决方法。

      为了测试这个脚本,我输入了lastRow = 3。将 3 更改为 sheet.getLastRow() 以完成您的完整工作表。

      另请注意,“getsheetbyname”使用单引号 'XXX',因为其中有一个空格,否则会破坏这一点。

      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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        • 2022-10-20
        • 1970-01-01
        • 2014-07-08
        • 2022-09-23
        • 2021-06-11
        相关资源
        最近更新 更多