Reputation: 3880
I am trying to import some product datas from Excel to Google Sheet. There are some pictures in excel. If I simply copy and paste the pictures will be missing out. With Google Sheet's import function, the images will appear in Google sheet just as they do in excel.
However, most of the pictures are "over the cell". I want them to be "in the cell" so it is easier to manipulate the pictures later.
I don't know why SOME of the product pictures appear to be "in the cell" in google sheet. The screenshot below is from Excel, the two selected pictures are inserted into excel in the same way:
And this is a screenshot from Google Sheet:
You can see in the last row the picture is "in the cell". If there is a way to ensure the pictures to be "in the cell" after the import, my problem will be solved. But I can't find out how.
So I am thinking if I can use App Script to do this, the steps I can think of are:
Is this possible at all?
A sample of my data: https://docs.google.com/spreadsheets/d/1OtlQNu7cKelnXBJk2gdCXHJkT0ixNugXfaBkQfQzxto/edit?usp=sharing
Upvotes: 0
Views: 2260
Reputation: 201378
In the current stage, unfortunately, there is no built-in method for directly retrieving the image in a cell using Google Apps Script. So, in this case, I would like to propose using DocsServiceApp (Author: me) of a Google Apps Script library. When this library is used, the image in a cell can be retrieved, and also it can confirm whether the image is put on a cell or into a cell. When this library is used as a sample script for achieving your goal, how about the following sample script?
Please install DocsServiceApp of a Google Apps Script library. You can see how to install it at here.
Please enable Drive API at Advanced Google services. Ref
Please copy and paste the following script to the script editor of your sample Spreadsheet.
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const obj = DocsServiceApp.openBySpreadsheetId(ss.getId()).getSheetByName("test").getImages();
const sheet = ss.getSheetByName("test");
obj.forEach(o => {
if (!o.image.innerCell) {
const temp = DriveApp.createFile(o.image.blob);
const link = Drive.Files.get(temp.getId()).thumbnailLink.replace("=s220", "=s1000");
sheet.getRange(o.range.a1Notation).setValue(SpreadsheetApp.newCellImage().setSourceUrl(link).build());
temp.setTrashed(true);
}
});
sheet.getImages().forEach(e => e.remove());
}
From your following reply,
It works on my test sheet. However when I try to run it on my actual sheet, I am running into this error:
Although, unfortunately, I cannot replicate your situation, from your error message, I added one more sample script by guessing your current situation. Could you please confirm it?
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ssId = ss.getId();
const tempSS = DriveApp.getFileById(ssId).makeCopy("temp");
const obj = DocsServiceApp.openBySpreadsheetId(tempSS.getId()).getSheetByName("test").getImages();
const sheet = ss.getSheetByName("test");
obj.forEach(o => {
if (!o.image.innerCell) {
const temp = DriveApp.createFile(o.image.blob);
const link = Drive.Files.get(temp.getId()).thumbnailLink.replace("=s220", "=s1000");
sheet.getRange(o.range.a1Notation).setValue(SpreadsheetApp.newCellImage().setSourceUrl(link).build());
temp.setTrashed(true);
}
});
sheet.getImages().forEach(e => e.remove());
tempSS.setTrashed(true);
}
Upvotes: 2