Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Image into Spresheet Cell from Drive using Google Apps Script

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 that image files should be shared publicly on the internet. However, I am working with digital assets and can not share them publicly.

I have the following code, this works but does not add to the required cell. Is this at all possible?

function insertImageFromDrive(){
 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  
  var fileId = '0B5UAkV0avfiKNGYtd1VzUzlsTVk';
  var img = DriveApp.getFileById(fileId).getBlob();
 
  
  sheet.insertImage(img, 4, 3)
}
like image 604
Graham Bourne Avatar asked Dec 07 '16 14:12

Graham Bourne


People also ask

How do I assign an image to a script in Google Sheets?

Return to Sheets and insert an image or drawing by selecting Insert > Image or Insert > Drawing. After inserting the image or drawing, click it. A small drop-down menu selector will appear in the top right-hand corner. Click it and choose Assign script.

What is the shortcut to insert a picture into a cell in Google Sheets?

On Mobile: Tap once on a cell to select. Tap again to bring up menu: Insert > Tap the “+” at the top of the screen > Image > Image in cell. Select an image from the options presented to you.


3 Answers

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);
like image 164
noogui Avatar answered Sep 28 '22 10:09

noogui


There is a google spreadsheet add-on ImageKit to help insert multiple images from difference sources including Google Drive, check it out > https://chrome.google.com/webstore/detail/imagekit/cnhkaohfhpcdeomadgjonnahkkfoojoc Here you find a screenshot of tool's interface. imagekitAddon

like image 20
Albert Avatar answered Sep 28 '22 11:09

Albert


I don't think it is currently possible, see here: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3303

like image 24
utphx Avatar answered Sep 28 '22 12:09

utphx