I would like to be able to scale an image (from a google form response) down before or after inserting it into my google doc. I've been trying to use the ImgApp library with the .doResize()
method. But I'm not quite sure how it's supposed to work.
Here is what I've got currently. Have I resized the image from google form correctly?
var fileURL = photo;
var fileID = fileURL.substr(fileURL.search("=") 1); //strip off text before id= in the URL
var image = DriveApp.getFileById(fileID).getBlob();
var imageR = ImgApp.doResize(fileID, 630)
image = imageR.blob;
Now the question is how do I insert the resized image?
below is my full script
function testAutoFillGoogleDocFromForm() {
try {
let row = { values: [ "time", "photo", "2/1/2022", "event", "name", "", "photoCap", "photoDesc" ]};
autoFillGoogleDocFromForm(row);
console.log("done");
row = { values: [ "time", "photo", "1/1/2023", "event", "name", "", "photoCap", "photoDesc" ]};
autoFillGoogleDocFromForm(row);
console.log("done");
}
catch(err) {
console.log(err)
}
}
function autoFillGoogleDocFromForm(e) { //function to insert google form sheet responses from the linked google sheet to the google doc
try {
let timestamp = e.values[0];
let photo = e.values[1];
let date = new Date(e.values[2]);
let event = e.values[3];
let name = e.values[4];
let photoCap = e.values[6];
let photoDesc = e.values[7];
var headingStyle = {};
headingStyle[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.HEADING1;
try {
var fileURL = photo;
var fileID = fileURL.substr(fileURL.search("=") 1); //strip off text before id= in the URL
var image = DriveApp.getFileById(fileID).getBlob();
var imageR = ImgApp.doResize(fileID, 630)
image = imageR.blob;
}
catch(err) {
console.log(err)
}
var doc = DocumentApp.openById("1x3M2BWD3saQdnuygS8CPyd7KOVrxkYBOs6s9p8GbImQ") //inserts responses based on date
let body = doc.getBody();
let i = 0;
while( i < body.getNumChildren() ) {
let para = body.getChild(i);
if( para.getType() === DocumentApp.ElementType.PARAGRAPH ) {
let j = 0;
console.log("numchild = " body.getNumChildren());
while( j < para.getNumChildren() ) {
let child = para.getChild(j);
if( child.getType() === DocumentApp.ElementType.PAGE_BREAK ) {
// get next paragraph and check date
if( (i 1) >= body.getNumChildren() ) break; // in case there is a page break at the end of body
para = body.getChild(i 1);
let temp = new Date(para.asParagraph().getText());
console.log(temp);
if( temp > date ) {
body.insertPageBreak(i );
body.insertParagraph(i ,date.toLocaleDateString()).setAttributes(headingStyle);
body.insertParagraph(i ,event);
body.insertParagraph(i ,'- ' name);
body.insertImage(i , image);
body.insertParagraph(i ,photoCap);
body.insertParagraph(i ,photoDesc);
return;
}
}
j ;
}
}
i ;
}
// if the date is latest just append a new page
body.appendPageBreak();
body.appendParagraph(date.toLocaleDateString()).setAttributes(headingStyle);
body.appendParagraph(event);
body.appendParagraph('- ' name);
body.appendImage(image);
body.appendParagraph(photoCap);
body.appendParagraph(photoDesc);
body.in
}
catch(err) {
console.log(err)
}
}
CodePudding user response:
Instead of
var image = DriveApp.getFileById(fileID).getBlob();
var imageR = ImgApp.doResize(fileID, 630)
image = imageR.blob;
Do
// get permanent link to thumbnail, via https://stackoverflow.com/a/31504086/555121
// set `sz` to needed width in pixels
var res = UrlFetchApp.fetch('https://drive.google.com/thumbnail?sz=w630&id=' fileID, {
// authorize request with Apps Script credentials of current user
headers: {'Authorization': 'Bearer ' ScriptApp.getOAuthToken()}
});
// get thumbnail blob
var image = res.getBlob();
Reference: