AppsScript - Replicate “put image in selected cell” on Google Sheets

前端 未结 1 1155
轻奢々
轻奢々 2021-01-24 21:31

I am currently inserting multiple images in a Google Sheets via AppsScript function InsertImage

With it the function inserts the image but above the cell (not inside the

相关标签:
1条回答
  • 2021-01-24 22:20

    IssueTracker:

    There is currently no method in apps script to insert a image in cell. There is a open feature request here. Consider adding a star(on top left) to it for Google to prioritize the issue.

    Workaround:

    Here we use =IMAGE() formula to insert a image and copy paste values only to get the raw image. This is mentioned by me here and in comment#65 of the issuetracker.

    Sample script:

    const insertInCellImage_ = (url, rg) => {
      rg.setValue(`=IMAGE("${url}")`);
      SpreadsheetApp.flush();
      rg.copyTo(rg, { contentsOnly: true });
    };
    
    /*Inserts Google logo in Sheet1!A1*/
    const test1 = () =>
      insertInCellImage_(
        'https://www.google.com/images/srpr/logo3w.png',
        SpreadsheetApp.getActive()
          .getSheetByName('Sheet1')
          .getRange(1, 1)
      );
    
    0 讨论(0)
提交回复
热议问题