Home > Enterprise >  Google Apps Script : Iterate over blob to create multiple image files
Google Apps Script : Iterate over blob to create multiple image files

Time:11-26

I would like to iterate over a list of addresses I have in a spreadsheet and save them as images in my Drive. Ideally, I would like to save them in different folders depending on the value in a second column but this would be the next step.

In my first code I can save an image from one address in one row in my spreadsheet by naming the range as a variable and then saving the variable as a blob.

function savemap() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var addresses = sheet.getRange(1,1,13,1).getValues()
  var address = sheet.getRange(4,1).getValue()
  
  var map = Maps.newStaticMap().setMapType(Maps.StaticMap.Type.SATELLITE).setCenter(address).setZoom(20).getBlob()
  DriveApp.createFile(map)
}

However in second code, while I iterate through the addresses, I'm not sure how to attribute a blob for each address.

function save_adresses() {

var sheet = SpreadsheetApp.getActiveSheet();
var addresses = sheet.getRange(1,1,13,1).getValues()

for (var i = 0; i <= 8; i = i   1) {
  
  Maps.newStaticMap()
  .setMapType(Maps.StaticMap.Type.SATELLITE)
  .setCenter(addresses[i])
  .setZoom(19)
  .getBlob()
  .DriveApp.createFile(addresses[i])
  .getAs('image/png')

}
}

I get the following error :

TypeError: Cannot read property 'createFile' of undefined save_adresses @ practice2.gs:13

Any idea on how I can save each blob as a png? I'm sure this is something obvious but I would greatly appreciate feedback!

Many thanks.

CodePudding user response:

Have you tried something more similar to your already working first code but in between your for loop?

for (var i = 0; i <= 8; i = i   1) {
    var map = Maps.newStaticMap()  
       .setMapType(Maps.StaticMap.Type.SATELLITE)
        .setCenter(addresses[i])
        .setZoom(20)
        .getBlob()
     DriveApp.createFile(map)
}

And you're getting a range of 13 rows but only iterating 9 times. You may want to change the header of your for loop:

for (var i = 0; i < addresses.length; i = i   1) {
  • Related