How about this answer?
Hypothesis and Experiment:
From my experiment, in the Spreadsheet, when an image is inserted by insertImage()
in Class Sheet, the limitation is due to the image area (pixels^2) rather than the file size of it. The maximum area of image which can be inserted is 1,048,576 pixels^2. Ref
From this situation, I thought that your issue is also related to the case of Spreadsheet. The limitation of insertImage()
in Class Body might be due to the area of image. So I experimented as follows.
As the result, it was found that the limitation of area is 25,000,000 pixels^2.
About your issue:
In your case, the image size of the shared image is 6240 pixels x 4160 pixels. This area is 25,958,400 pixels^2. This area is larger than the value (25,000,000 pixels^2) retrieved above experiment. By this, it is considered that the error occurs.
Workaround:
In order to avoid this limitation, how about this workaround? In this workaround, when the image size is more than 25,000,000 pixels^2, the image size is decreased using Drive API. The modified script is as follows.
Modified script:
function myFunction() {
var folder = DriveApp.getFolderById(id);
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var img = file.getBlob();
// --- Following script was added.
var url = "https://www.googleapis.com/drive/v3/files/" + file.getId() + "?fields=imageMediaMetadata(height%2Cwidth)%2CthumbnailLink&access_token=" + ScriptApp.getOAuthToken();
var res = UrlFetchApp.fetch(url).getContentText();
var obj = JSON.parse(res);
if (obj.imageMediaMetadata && (obj.imageMediaMetadata.width * obj.imageMediaMetadata.height) > 25000000) {
var width = 1000;
var tlink = obj.thumbnailLink.replace(/=s\d+/, "=s" + width);
img = UrlFetchApp.fetch(tlink).getBlob();
}
// ---
var imgDoc = DocumentApp.getActiveDocument().getBody().insertImage(0, img);
}
}
- In this modification, as a sample, the image size is reduced with the width of 1000 pixels. If you want to modify this, please modify
var width = 1000
.
- I think that in your script, Drive API has already been automatically enabled because the class DriveApp is used. But if the error related to Drive APi occurs, please check whether Drive API is enabled at API console.
References: