I am trying to have an app script that will put an image from an online url into a cell on my workbook.
I do this using the =IMAGE(<image url>)
formula in my apps script.
Afterwards I use the .copyTo
function on the same cells to remove the function but keep the image.
The problem is the image disappears when I do this. The only way for me to get it to work is if I have a random variable assignment in between the code where I set the image functions and where I do the copy-pasting. It makes no sense to me why the code works with this additional line.. any ideas?
function insertImage() {
var sheet = SpreadsheetApp.getActiveSheet()
var data = sheet.getRange("A1").getValue()
if (data < 2 ){
sheet.getRange("B1").setFormula('=image("https://lh3.googleusercontent.com/l6JAkhvfxbP61_FWN92j4ulDMXJNH3HT1DR6xrE7MtwW-2AxpZl_WLnBzTpWhCuYkbHihgBQ=w640-h400-e365")')
sheet.getRange("B2").setFormula('=image("https://lh3.googleusercontent.com/l6JAkhvfxbP61_FWN92j4ulDMXJNH3HT1DR6xrE7MtwW-2AxpZl_WLnBzTpWhCuYkbHihgBQ=w640-h400-e365")')
sheet.getRange("B3").setFormula('=image("https://lh3.googleusercontent.com/l6JAkhvfxbP61_FWN92j4ulDMXJNH3HT1DR6xrE7MtwW-2AxpZl_WLnBzTpWhCuYkbHihgBQ=w640-h400-e365")')
var img = sheet.getRange("B3").getValue() // doesn't work without this line
sheet.getRange('B1').copyTo(sheet.getRange('B1'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sheet.getRange('B2').copyTo(sheet.getRange('B2'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sheet.getRange('B3').copyTo(sheet.getRange('B3'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
}
As long as the var img = ....
is there, the code works. Removing it makes the images disappear.
I'd like to not have that line since it doesn't do anything and have the images remain in the cell.