【问题标题】:AppsScript - Replicate "put image in selected cell" on Google SheetsApps 脚本 - 在 Google 表格上复制“将图像放入选定的单元格”
【发布时间】:2020-09-12 10:48:14
【问题描述】:

我目前正在通过 AppsScript 函数 InsertImage 在 Google 表格中插入多张图片

使用它,该函数将图像插入到单元格上方(而不是单元格内)。 我可以通过单击三个点按钮将该图像插入到单元格中,然后选择“将图像放入选定的单元格”,就像在这个 link 中一样。

这里的问题是如何通过 AppsScript(或其他编码/脚本方法)复制这种“将图像放入选定单元格”的行为,以便我可以自动执行此操作。

我不能选择使用公式 IMAGE,因为该公式不允许我在使用 iOS 表格应用程序时看到插入的图像...

【问题讨论】:

  • 不幸的是,我在 Apps Script 和 Sheets API 中都没有看到这个功能:/
  • as that formula doesn't allow me to see the inserted images when using iOS sheets apps... ???
  • 我的意思是,一旦我使用 IMAGE(url) 公式从现有 URL 插入图像,则该图像不会显示在 iOS 的 SHEETS 应用程序中(例如)(@987654323 @)
  • @byle.05 我的解决方法是否适用于表格应用程序?

标签: google-apps-script google-sheets


【解决方案1】:

问题跟踪器:

应用脚本中目前没有在单元格中插入图像的方法。有一个开放的feature request here。考虑为其添加星号(左上角),以便 Google 确定问题的优先级。

解决方法:

在这里,我们使用=IMAGE() 公式插入图像并复制粘贴值仅用于获取原始图像。我在here 和 issuetracker 的comment#65 中提到了这一点。

示例脚本:

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

【讨论】:

    【解决方案2】:

    考虑以下几点:

        function insertImageIntoCell()
        {
          let image = SpreadsheetApp.newCellImage().setSourceUrl('https://www.gstatic.com/images/branding/product/2x/apps_script_48dp.png').setAltTextDescription('Google Apps Script logo').toBuilder().build();
    
      SpreadsheetApp.getActive().getActiveSheet().getRange('A1').setValue(image);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多