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()
}