Insert Image into Spresheet Cell from Drive using Google Apps Script

后端 未结 5 1112
名媛妹妹
名媛妹妹 2020-12-06 08:29

I would like to be able to add an image file into my spreadsheet from Google Drive. I see there is a built-in image function available =image, but this requires a URL and

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 08:35

    Try using the guide Insert image in a spreadsheet from App Script:

    function insertImageOnSpreadsheet() {
      var SPREADSHEET_URL = 'INSERT_SPREADSHEET_URL_HERE';
      // Name of the specific sheet in the spreadsheet.
      var SHEET_NAME = 'INSERT_SHEET_NAME_HERE';
    
      var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
      var sheet = ss.getSheetByName(SHEET_NAME);
    
      var response = UrlFetchApp.fetch(
          'https://developers.google.com/adwords/scripts/images/reports.png');
      var binaryData = response.getContent();
    
      // Insert the image in cell A1.
      var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
      sheet.insertImage(blob, 1, 1);
    }
    

    Just replace the necessary values. The part where you indicate which cell you insert the image is in:

    sheet.insertImage(blob, 1, 1);
    

提交回复
热议问题