-2

I added an image to a cell successfully, but if I assigned a function programmatically it doesn't appear anymore.

const data = Utilities.newBlob(Utilities.base64Decode("<my base64 string>", "image/png", "run")
const image = sheet.insertImage(data, 1, 4)

This will correctly show the image in that cell. When I add:

image.assignScript("show")

the image doesn't appear anymore. show is an existing function. If I assign that script manually to the image it works correctly.

Edit: the same is happening if I use setWidth or setHeight in place of assignScript

Edit(2): I've just found that it's disappearing only on the first execution. If I refresh the page then it works correctly but it always needs the refresh to work properly.

Some more code details: From menu I call a function which in the end calls the addButton function:

function onOpen(){
  const UI = SpreadsheetApp.getUi()
  ui.createMenu('My menu')
    .addSubMenu(ui.createMenu('Templates')
    .addItem('Item 1', 'myFunctionForItem1')
    ...
  ).addToUi()
}

function myFunctionForItem1() {
  SpreadsheetApp.getActiveSpreadsheet().insertSheet("title",0)
  const sheet = SpreadsheetApp.getActiveSheet()
  //insert some templates (arrays of strings)
  sheet.getRange(1,1,template.length,template[0].length).setValues(template)
  SpreadsheetApp.flush()
  addRunButton(2, 1, 'show')
}

function addRunButton(row, column, fun) {
  const sheet = SpreadsheetApp.getActiveSheet()
  const data = Utilities.newBlob(Utilities.base64Decode("<my base64 string>", "image/png", "run")
  const image = sheet.insertImage(data, column, row)
  SpreadsheetApp.flush()
  image.assignScript(fun)
  SpreadsheetApp.flush()
}
al1812
  • 340
  • 1
  • 6
  • 17
  • I tried this with the minimal amount of code to be like your sample and the image didn't disappear. Can you share the rest of your relevant code? – Daniel Apr 07 '23 at 20:02
  • Hey @Daniel, I shared some more code, and maybe another important detail (look at Edit(2)) – al1812 Apr 07 '23 at 21:50
  • 2
    I thought that this thread might be an answer to your question. https://stackoverflow.com/q/57248310 How about this? – Tanaike Apr 08 '23 at 00:04
  • Hi @Tanaike, yes thanks, that's exactly the same problem. I got inspiration from your answer and slightly improved it. Although I'm not so happy of final result, it's still the best workaround I found – al1812 Apr 08 '23 at 12:16
  • Thank you for replying. I'm glad your issue was resolved. From your reply of `that's exactly the same problem.`, I flagged it as a duplicate question. – Tanaike Apr 08 '23 at 12:18
  • Ok, I'll post my solution on that question – al1812 Apr 08 '23 at 12:23

1 Answers1

0

This is exactly the same issue mentioned by @Tanaike. His answer inspired me to find a workaround, but this is clearly a bug in Google Apps Scripts, and couldn't completely fix it.

As Tanaike said, the button appears after a refresh or after a tab switch. I slightly improved his solution: basically, I create a new sheet and hide it immediately, then fill it (with also image) and finally show it to the user. This is a small improvement that allows for reducing the flickering between tabs.

const sheet = spreadsheet.insertSheet(title, 0).hideSheet()
// fill sheet
sheet.showSheet()
Spreadsheet.setActiveSheet(sheet)
E_net4
  • 27,810
  • 13
  • 101
  • 139
al1812
  • 340
  • 1
  • 6
  • 17
  • If you added a answer there, you may delete this one. As to downvotes, I can only speculate that it's due to a lack of research/[mcve] in the first version of your question. People probably wasted time trying to reproduce the issue: In the future, always add the complete code(not just snippets) and make sure it's reproducible in a new sheet and script, i.e., all the edits to the question should've been in the initial version of your question. – TheMaster Apr 08 '23 at 14:49