1

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!

123
  • 595
  • 6
  • 18
  • I wonder if the new PositionedImages would be faster? – Mogsdad Dec 14 '15 at 03:31
  • I'll have to take a look into that, hadn't heard of it, thank you – 123 Dec 14 '15 at 03:38
  • Your blog showed up at the top of the search, interestingly enough :) – 123 Dec 14 '15 at 03:41
  • It seems to take the same amount of time (9.5 seconds for 10 images). However, the execution transcript shows each PositionedImages operation taking 0 seconds... which definitely does not add up to 9.5 seconds. Probably something Google will fix later? – 123 Dec 14 '15 at 04:32
  • 1
    ...or just one more miss in that feature's roll-out. Too bad it's not faster, though. I would have expected the UrlFetch to have been the long pole. Unfortunately, I think you're stuck with this for now - any performance improvement will have to come from Google. – Mogsdad Dec 14 '15 at 04:42
  • Oh well, it's relatively manageable. Thank you for your help! – 123 Dec 14 '15 at 08:50

0 Answers0