Reputation: 55
I would like to remove/delete an image that I inserted into a google sheet. I used the code:
sheet.insertImage(url,col,row);
to insert the image. But, I want to remove the image later in the code and insert a different image. or not have an image at all.
Upvotes: 5
Views: 7032
Reputation: 1
//If you want to delete a specific image located in a known cell, this will do the trick.
const imagins = sheet.getImages();
for (let i = 0; i < imagins.length; i++) {
if (row == imagins[i].getAnchorCell().getRow() &&
col == imagins[i].getAnchorCell().getColumn()) {
imagins[i].remove();
break;
}
}
Upvotes: 0
Reputation: 342
Try the following:
// Deletes all images in sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Your Tab Name");
var images = sheet.getImages();
images.map(function(img){img.remove();});
Upvotes: 7
Reputation: 21
In my testing, inserting a different image, in the the same cell, simply stacks the new image over the top of the old image.
sheet.insertImage('http://somewhere.com/images/image1.png',col,row);
sheet.insertImage('http://somewhere.com/images/image2.png',col,row);
I had hoped to effectively 'remove' image1 by substituting a blank(transparent) image2. One can cover up image1 with some other opaque image. ('mask', 'overlay', 'overlap' 'conceal', 'hide')
overlapped images In my experience, theCell.clearContent() does not affect the image but will clear other cell contents.
Upvotes: -1
Reputation: 4826
use this code to remove your image
function clearRange() {
//replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange('B7:G7').clearContent();
}
Upvotes: -1