Return CellImage from Custom Function in Apps Script

huangapple go评论70阅读模式
英文:

Return CellImage from Custom Function in Apps Script

问题

我正在尝试从Google Sheets自定义函数中返回一个CellImage。我在Apps Script中构建了图像:

function TESTIMG() {
    let oneImage = SpreadsheetApp
      .newCellImage()
      .setSourceUrl('https://bluewarelabs.files.wordpress.com/2022/10/cropped-bwl-logo-4.png')
      .build();

    return oneImage;
}

该函数没有出现错误,但它生成了一个空白单元格。然而,如果只是使用以下方式生成图像:

=IMAGE("https://bluewarelabs.files.wordpress.com/2022/10/cropped-bwl-logo-4.png")

那么它可以正常工作。

这是否是一个已知的错误?返回CellImage不起作用吗?

英文:

I'm trying to return a CellImage from a Google Sheets custom function. I build out the image in Apps Script:

function TESTIMG() {
    let oneImage = SpreadsheetApp
      .newCellImage()
      .setSourceUrl('https://bluewarelabs.files.wordpress.com/2022/10/cropped-bwl-logo-4.png')
      .build();

    return oneImage;
}

The function does not error out, but it produces blank cell. However, if just generate the image using

=IMAGE("https://bluewarelabs.files.wordpress.com/2022/10/cropped-bwl-logo-4.png")

It works fine.

Is this a known bug? Returning a CellImage not working?

答案1

得分: 1

如上面的评论中提到的,要将CellImageBuilder类的对象放入单元格作为图像对象,需要使用setValuesetValues进行设置。您可以在此处提交一个功能建议:点击此处

我猜想还有其他方法可以实现您的目标,但为了给您一个想法,我想到了以下的解决方法:

function insertImage()
{
  let images = [
    'https://bluewarelabs.files.wordpress.com/2022/10/cropped-bwl-logo-4.png',
    'https://developers.google.com/google-ads/scripts/images/reports.png',
    'https://i.imgur.com/gtfe7oc.png',
    'https://www.google.com/images/srpr/logo3w.png'
  ]

  let column = SpreadsheetApp.getActiveRange().getColumn();
  let row = SpreadsheetApp.getActiveRange().getRow();

  for (let i=0; i<images.length; i++){
    //使用此选项插入IMAGE公式
    SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(row, column+i).setFormula('=IMAGE("' + images[i] + '")');

    //如果您想要使用newCellImage方法进行操作,可以使用此选项
    // let oneImage = SpreadsheetApp.newCellImage().setSourceUrl(images[i]).build().toBuilder();
    // SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(row, column+i).setValue(oneImage);
  }
}

由于Google Sheets中自定义函数存在一些限制,此解决方法不能用作自定义函数。相反,您可以在菜单中添加一个选项,然后从菜单中运行该函数,以避免手动运行脚本。要在Google Sheets菜单中添加选项,您可以在脚本中添加以下内容:

function onOpen( ){
  // 这一行调用了SpreadsheetApp并获取其UI
  var ui = SpreadsheetApp.getUi();
 
  // 这些行创建了菜单项并将它们与我们将在Apps Script中编写的函数绑定起来
  ui.createMenu('Custom Functions')
      .addItem('Instert Images', 'insertImage')
      .addSeparator()
      .addToUi();
}

运行onOpen()函数,然后返回您的Google表格,您将在菜单中看到新的选项:

Return CellImage from Custom Function in Apps Script

参考链接:

英文:

As mentioned in the comments above, in order to put the Class CellImageBuilder object into a cell as the image object, it is required to put it using setValue or setValues. You can submit a feature idea here.

I guess there are other ways to achieve your goal but to give you an idea, I thought of the following workaround:

function insertImage()
{
  let images = [
    &#39;https://bluewarelabs.files.wordpress.com/2022/10/cropped-bwl-logo-4.png&#39;,
    &#39;https://developers.google.com/google-ads/scripts/images/reports.png&#39;,
    &#39;https://i.imgur.com/gtfe7oc.png&#39;,
    &#39;https://www.google.com/images/srpr/logo3w.png&#39;
  ]

  let column = SpreadsheetApp.getActiveRange().getColumn();
  let row = SpreadsheetApp.getActiveRange().getRow();

  for (let i=0; i&lt;images.length; i++){
    //Use this option to insert the IMAGE formula
    SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(row, column+i).setFormula(&#39;=IMAGE(&quot;&#39;+ images[i] +&#39;&quot;)&#39;);

    //You can use this option if you want to do it with the newCellImage method
    // let oneImage = SpreadsheetApp.newCellImage().setSourceUrl(images[i]).build().toBuilder();
    // SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(row, column+i).setValue(oneImage);
  }
}

Due to some limitations with custom functions in Google Sheets, this workaround can't be used as a custom function. Instead you can add an option in the menu and run the function from there so you don't have to run the script manually. To add the option in the Google Sheets menu, you can add the following in your script:

function onOpen( ){
// This line calls the SpreadsheetApp and gets its UI   
// Or DocumentApp or FormApp.
  var ui = SpreadsheetApp.getUi();
 
//These lines create the menu items and 
// tie them to functions we will write in Apps Script
  
 ui.createMenu(&#39;Custom Functions&#39;)
      .addItem(&#39;Instert Images&#39;, &#39;insertImage&#39;)
      .addSeparator()
      .addToUi();
}

Run the onOpen() function and go back to your Google Sheet and you will see the new option in the menu:

Return CellImage from Custom Function in Apps Script

References:

huangapple
  • 本文由 发表于 2023年6月19日 21:32:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定