I am using this code (from an answer to a StackOverflow question):
function insertImage(barcodesObject) {
// Create a document.
var doc = DocumentApp.openById("DOCUMENT ID HERE");
Logger.log(JSON.stringify(barcodesObject))
for(var key in barcodesObject) {
// Get the URL string within each Object property (property name is the barcode text, eg. "Testing" barcode is barcodeObjects.Testing)
var URL = barcodesObject[key];
// Retrieve an image from the web.
var resp = UrlFetchApp.fetch(URL);
// Append the image to the first paragraph.
doc.getChild(0).asParagraph().appendInlineImage(resp.getBlob());
}
}
to insert barcode images into a separate Google Doc.
My problem: Each image iteration through the for
loop takes about 1 second (3 images = 3 seconds to insert into Doc), meaning a batch of 50 images takes a minute to process!
Seems like theses are the two culprits, according to the Execution Transcript:
UrlFetchApp.fetch([http://MyBarcodeGenerator]) [0.276 seconds]
Paragraph.appendInlineImage([Blob]) [0.836 seconds]
Note: I tried moving the DocumentApp.openById() in and out of the for
loop, but it stayed the same time, so it seems irrelevant to the long processing times
My Question: Is there any way to "optimize" this process, so it takes less time to add the image?
Thanks in advance!