I'm trying to paste png images from my drive into a spreadsheet. Lots of articles say that insertImage()
on Sheet Class will achieve this but when I run the code below, I keep getting a Service error where insertImage() is called. I googled it and looked it up here as well but haven't been able to find the solution yet. What am I doing it wrong?
function pastePhotosFromDrive(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("画像");
var folderId = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";
var files = DriveApp.getFolderById(folderId).getFiles();
var row = 1;
var col = 1;
Logger.log(files);
// var file1 = files.next();
// var blob1 = file1.getBlob());
while(files.hasNext()){
var file = files.next();
var blob = file.getBlob();
sheet.insertImage(blob, col, row);
row += 3;
if(row >= 25){
col = 0;
row += 20;
}
}
}
How about this answer?
I thought that in your situation, it is considered that the following 2 situations might occur.
As one of several workarounds, I would like to propose to modify the size of the image by selecting the image file.
When your script is modified, please modify as follows.
Before you use this script, please enable Drive API at Advanced Google Services .
while(files.hasNext()){
var file = files.next();
var blob = file.getBlob();
sheet.insertImage(blob, col, row);
row += 3;
if(row >= 25){
col = 0;
row += 20;
}
}
while(files.hasNext()){
var file = files.next();
if (file.getMimeType().indexOf("image") > -1) { // Here, the image file is selected.
var blob;
var obj = Drive.Files.get(file.getId());
var imageMetadata = obj.imageMediaMetadata;
if (imageMetadata.height * imageMetadata.width > 1048576) {
var width = 500; // Here, as a test case, 500 pixels is used for the width.
var tlink = obj.thumbnailLink.replace(/=s\d+/, "=s" + width);
blob = UrlFetchApp.fetch(tlink).getBlob();
} else {
blob = file.getBlob();
}
sheet.insertImage(blob, col, row);
row += 3;
if(row >= 25){
col = 0;
row += 20;
}
}
}
If I misunderstood your question and this was not the result you want, I apologize.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.