Insert image from sheets into docs using GAS

496 views
Skip to first unread message

Misael Rodriguez

unread,
Oct 17, 2022, 6:17:23 PM10/17/22
to Google Apps Script Community
Hi experts, 

As the title states, I need to Insert various images from sheets into docs using GAS to create a "Ticket".

this is the code I have so far... but I just can't get it to work. Any help would be appreciated.

function createBulkPDFs(){

 const docFile = DriveApp.getFileById("1hjNxk3f5beqXOgo8PQUfQEZnwbvreGP0tKTjhAExaQ4");
 const tempFolder = DriveApp.getFolderById("1GlJHRbupM0j-7J4lYiViiNQWB8gP_S_O");
 const pdfFolder = DriveApp.getFolderById("1zcIaMXV5ioJ0tLtEaQYRIrKr7RPpdZPI");
 const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("People");

 const data = currentSheet.getRange(2, 1,currentSheet.getLastRow()-1,5).getDisplayValues();
 
 let errors = [];
 data.forEach(row => {
   try{
    createPDF(row[0],row[1],row[2],row[3],row[0] + " " + row[1],docFile,tempFolder,pdfFolder);
    errors.push([""]);
   } catch(err){
     errors.push(["Failed"]);
   }
 });

 currentSheet.getRange(2, 5,currentSheet.getLastRow()-1,1).setValues(errors);
}



function createPDF(firstName,uniqueID,funcion,qrCode,pdfName,docFile,tempFolder,pdfFolder,currentSheet) {
 
 const tempFile = docFile.makeCopy(tempFolder);
 const tempDocFile = DocumentApp.openById(tempFile.getId());
 const body = tempDocFile.getBody();
 body.replaceText("{first}", firstName);
 body.replaceText("{unique}", uniqueID);
 body.replaceText("{qrcode}", qrCode);
 body.replaceText("{funcion}", funcion);
 
 // Convert image to blob
 const range = currentSheet.getRangeByName("qrcode");
 const cellImage = range.getValues();
 const blob = cellImage.getBlob();
 body.insertImage(0, blob);
   

 tempDocFile.saveAndClose();
 const pdfContentBlob = tempFile.getAs(MimeType.PDF);
 pdfFolder.createFile(pdfContentBlob).setName(pdfName);
 tempFolder.removeFile(tempFile);

}


Capture1.PNG
Capture2.PNG

Tanaike

unread,
Oct 17, 2022, 11:24:41 PM10/17/22
to Google Apps Script Community
I think that in your situation, it is required to know the detail of images in your actual Spreadsheet. How were your images put into the cells?

Misael Rodriguez

unread,
Oct 18, 2022, 1:23:01 PM10/18/22
to Google Apps Script Community
Hi, thank you for your reply... 

They are both being put into the cells via the IMAGE Function

=IMAGE("URL")

Tanaike

unread,
Oct 18, 2022, 8:05:15 PM10/18/22
to Google Apps Script Community
Thank you for replying. From your reply, in this case, I think that you can retrieve the URL using this sample script https://stackoverflow.com/q/23750321

When the URL is retrieved, you can retrieve the image blob using UrlFetchApp.

Misael Rodriguez

unread,
Oct 19, 2022, 6:59:11 PM10/19/22
to Google Apps Script Community
Thank you for trying to help me, I still can't figure it out lol. I will continue to try.

Misael Rodriguez

unread,
Oct 19, 2022, 7:42:04 PM10/19/22
to Google Apps Script Community
Here is access to the spreadsheet (its a test anyway)

and the files

In case you have the time to look through... thanks in advance!

Tanaike

unread,
Oct 19, 2022, 8:44:13 PM10/19/22
to Google Apps Script Community
Thank you for replying. When I saw your sample Spreadsheet and your script, in this case, it is required to retrieve both the values and formulas. Because your formula is like `=IF(ISBLANK(#REF!)," ",IMAGE("https://chart.googleapis.com/chart?cht=qr&chs=312x312&chl="&B2))`.

In order to retrieve the URLs from your Spreadsheet, a simple sample script is as follows.

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("People");
const lastRow = sheet.getLastRow();
const values = sheet.getRange("B2:B" + lastRow).getValues();
const formulas = sheet.getRange("D2:D" + lastRow).getFormulas();
const urls = values.map(([b], i) => formulas[i][0].match(/IMAGE\("(.+)"/)[1] + b);
console.log(urls)

By this, the URLs for retrieving the QR codes can be retrieved. If this was not useful, I apologize.

Misael Rodriguez

unread,
Oct 20, 2022, 7:52:41 AM10/20/22
to Google Apps Script Community
Thank you so much for your help... and I thought I had it all figured out :D

But if I could impose on you (hopefully one last time). I am just very new at this scripting.

I was able to get the image to show in my Doc and ultimately create the PDF, but I noticed two things (and I tried to resolved them, but I just couldn't figure it out)
1. The image goes above the table, not where I thought it would go (and it creates a two page document)
2. It puts the same QR Code image in all of them (not each unique one)

Thank you in advance and I really appreciate all the help so far (I am learning quite a bit).

Tanaike

unread,
Oct 20, 2022, 8:38:39 PM10/20/22
to Google Apps Script Community
Thank you for replying. I'm glad your issue was resolved.

Reply all
Reply to author
Forward
0 new messages