I want to get text from an image placed in a cell using Google Apps Script. What code do I write in Google Apps Script?enter image description here
Can the cell be saved as an image and the text read?
I want to get text from an image placed in a cell using Google Apps Script. What code do I write in Google Apps Script?enter image description here
Can the cell be saved as an image and the text read?
I believe your goal is as follows.
Unfortunately, in the current stage, there are no built-in methods for directly retrieving the images in the cells. So, in this case, I use this workaround for retrieving the images from the cells. And, in order to convert the texts from the image, Drive API is used.
When these are reflected in a sample script, it becomes as follows.
In this workaround, DocsServiceApp (Author: me) of a Google Apps Script library is used. Please install this library. You can see how to install it at here.
Please copy and paste the following script to the script editor of Spreadsheet. And, please set your sheet name.
function myFunction() {
const sheetName = "Sheet1"; // Please set your sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const spreadsheetId = ss.getId();
const ar = DocsServiceApp.openBySpreadsheetId(spreadsheetId).getSheetByName(sheetName).getImages();
const res = ar.map(({ range, image }) => {
const tempId = Drive.Files.insert({ mimeType: MimeType.GOOGLE_DOCS, title: "temp" }, image.blob).id;
const text = DocumentApp.openById(tempId).getBody().getText();
Drive.Files.remove(tempId);
return { cell: range.a1Notation, text: text.trim() };
});
console.log(res);
}
Please enable Drive API at Advanced Google services. Ref
When the above script is run for your provided Spreadsheet, the following result can be seen in the log.
[
{"cell":"A1","text":"098765431 ZYX"},
{"cell":"A2","text":"1234567890 abcdef"},
{"cell":"A3","text":"098765431 ZYX"},
{"cell":"A4","text":"1234567890 abcdef"},
{"cell":"A5","text":"1234567890 abcdef"},
{"cell":"A6","text":"098765431 ZYX"},
{"cell":"A7","text":"098765431 ZYX"}
]