I'm tryna create a google app script code that will retrieve image files from a folder and insert them into cells in a column.
All sheets within this spreadsheet have the same template, so all getRange() rows and columns are correct. All images within all folders that I'm trying to retrieve images from have standardised dimensions of 1,202 x 720 pixels (98KB), are in JPG format, and its names are just Slide1.JPG, Slide2.JPG, etc.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
function insertImgsFromDriveFolder() {
Logger.log('sheet name: ' + sheet.getSheetName());
Logger.log('Drive Folder ID cell: ' + sheet.getRange(1,4).getA1Notation());
var folderID = sheet.getRange(1,4).getValue();
Logger.log('Drive Folder ID: ' + folderID);
var folder = DriveApp.getFolderById(folderID);
var files = folder.getFiles(); // retrieves Iteration of files, sometimes in reverse order
// so must get count of no items in folder
var count = 0;
while (files.hasNext()) {
var file = files.next();
count++;
}
// then search for each item by name + display in sheet
for (let i=1; i <= count; i++) {
var imgFile = folder.getFilesByName("Slide" + i + ".JPG").next();
//imgFile.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
Logger.log('filename: ' +imgFile);
img = SpreadsheetApp.newCellImage().setSourceUrl('https://drive.google.com/uc?export=view&id=' + imgFile.getId()).build();
Logger.log('file: ' +img);
sheet.getRange(i+4,2).setValue(img);
}
}
The error message I receive in the Execution log is "Exception: Error retrieving image from URL or bad URL: " on the sheet.getRange(i+4,2).setValue(img); line.
I am able to view the image by copying that URL from the error in Chrome's Incognito Mode (no google account signed in), so I think the file is publicly-accessible. I am also able to manually insert an image into a cell using that URL using =IMAGE(). De-commenting imgFile.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT); to force it to be public-accessible results in the same error.
I also think that the file size is not an issue as it smaller than the size limit stated in this post.
I also do not want to use insertImage() as I want the images to be inserted into the cells. I have also tried the solutions provided in here, here and here.
Lmk what I can try to fix this issue! Also, if you also have a solution on how to force the FileIteration to take files in alphabetical/date-created order that would be awesome.
Thanks in advance!
Exception: Error retrieving image from URL or bad URL. 2. You want to retrieve the image files in a specific order. I think that I can understand your 1st question. But, unfortunately, I cannot understand your 2nd question. I cannot understandtake files in alphabetical/date-created order. Can I ask you about the detail of your 2nd question? First, I would like to correctly understand your questions.folder.getFiles();to retrieves the files? whether that order is by ascending/descending alphabetical order, or furthest/latest date creation, etc. Alternatively, is there a way I can sort the files in a folder prior to usingfolder.getFiles();?take files in alphabetical/date-created order. Can I ask you about the detail of it?take files in alphabetical/date-created order, do you want to retrieve the files with the ascending order of filename? Or, do you want to retrieve the files with the ascending order of the created date of the file? Or, do you want to sort the file list by the created date after the file list was sorted by the filename? I cannot understand which do you want to do. I apologize for my poor English skill again.