I apologize for the confusion earlier. It seems that range.getImages() is not a function provided by the Range class in Google Apps Script, and Google Sheets currently does not natively support extracting images directly from cells using Apps Script.
However, you can still access the image if it's a URL embedded in the cell or handle it as part of a Drawing object within the sheet.
Here's an updated approach to deal with these cases:
Case 1: Image as a URL in the Cell
If the image URL is present in the cell:
- Open your Google Sheets and go to
Extensions > Apps Script.
- Delete the default code and add the following:
function getImageURLFromCell() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Arkusz1");
var cell = sheet.getRange("A1");
var imageUrl = cell.getFormula().match(/"(http[^"]+)"/);
if (imageUrl) {
Logger.log(imageUrl[1]);
} else {
Logger.log("No URL found in the specified cell.");
}
}
- Save the script and run the
getImageURLFromCell function.
- Check the logs (View > Executions or
Logger.log in the script editor) to see the image URL.
Case 2: Directly Inserted Image as a Drawing
If the image is inserted directly as a drawing, you can locate it within the embedded Drawing objects:
function getImageFromDrawing() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Arkusz1");
var drawings = sheet.getDrawings();
// Iterate through all drawings to find one within the desired cell range
for (var i = 0; i < drawings.length; i++) {
var drawing = drawings[i];
var position = drawing.getContainerInfo().getAnchorCell();
if (position.getRow() == 0 && position.getColumn() == 0) { // A1 corresponds to (0,0)
var blob = drawing.getBlob();
var file = DriveApp.createFile(blob.setName('ExtractedImage.png'));
Logger.log("Image saved as: " + file.getUrl());
return;
}
}
Logger.log("No image found in the specified cell.");
}
This script searches all drawings in the sheet, checks which one is positioned at cell A1 (equivalent to row 0, column 0 in zero-based indexing), and then saves this drawing as an image file in Google Drive.
Running the Script
- Save and run the appropriate function (
getImageFromDrawing) based on how the image is embedded in the spreadsheet.
- Review the logs to get the image URL or confirmation that the image has been saved to Google Drive.
Handling Permissions
Make sure you grant the necessary permissions when prompted by the script, specifically for accessing Google Sheets and Google Drive.
This approach will help you retrieve images whether they are referenced by a URL or directly inserted as drawings in your Google Sheets.