user3404324
user3404324

Reputation: 41

Creating Forms Through Google App Script - Pulling Images Dynamically

The forms function used to generate items in a form seems to work dynamically for everything but images. For generating a video in the form, this is the format used, pulling data dynamically from a spreadsheet.

    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("makechoice");

    var dataRange = ss.getRange(2,1,40,20);
    var data = dataRange.getValues();


    var form = FormApp.create("Make a Test");  
    for (i in data) {
    var row = data[i];
    var qst = row[2];
    var url  = row[8];

    if (word == "Video") { form.addVideoItem()
                           .setTitle(qst)
                           .setHelpText(qst)
                           .setVideoUrl(url);
                         }

This works fine. However, the example given for images is as follows.

    var img = UrlFetchApp.fetch('http://www.example.com');
    form.addImageItem()
            .setTitle('Title')
            .setHelpText('Description') // The help text is the image description
            .setImage(img);

This does not allow anything other than a url to be used with the "fetch" function! Therefore, I'm only able to use one static url to get it work, but it can't pull from a cell in the spreadsheet like it would with the video example. Is there a way around this? I'd like this to generate multiple image items in the form dynamically and in the loop.

Upvotes: 1

Views: 1214

Answers (1)

Bryan P
Bryan P

Reputation: 5051

.setImage() takes a blob type

var img = UrlFetchApp.fetch('https://developers.google.com/apps-script/images/logo-transparent.png');
var blob = img.getBlob();
...
.setImage(blob);

Upvotes: 1

Related Questions