脚本 Google 表格错误,因为单元格为空

Script Google Sheets error because cell is empty

我尝试获取代码并根据我的需要对其进行调整,但没有成功,因为我是脚本编写的新手。

此代码允许生成 Google 地图的图像,其中每个地址都有标记和连接它们的线。 起初这段代码用于我们必须在代码中指明的 2 个地址,生成的图像通过电子邮件发送。

所以我重写了代码(我的第一个)来绘制和标记 6 个地址的点,然后将生成的图像粘贴到我的 sheet.

的单元格中

我遇到的问题是,当至少一个包含地址的单元格不包含值(因此该值是一个地址)时,我收到错误“异常:无效参数:地址”。我需要在 .getValue() 函数之后添加一些东西,但我不能。

我知道这个问题的答案对于高级脚本用户来说可能看起来很简单,但是这段代码可能对其他人有用。

感谢您的洞察和宽容。

我的表格:

https://docs.google.com/spreadsheets/d/1eZUlQK3-4WZmhQAIw5BTLsKkbTUheCTsdKEhdG0k87E/edit#gid=1047347094

我的脚本:

function onOpen() {
  SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
    .addItem(' Générer les Cartes', 'myFunction')
    .addToUi();
  myFunction()
}

function myFunction() {
  var sheet = SpreadsheetApp.getActive();
  var address1 = sheet.getRange('K121').getValue();
  var address2 = sheet.getRange('K122').getValue();
  var address3 = sheet.getRange('K123').getValue();
  var address4 = sheet.getRange('K124').getValue();
  var address5 = sheet.getRange('K125').getValue();
  var address6 = sheet.getRange('K126').getValue();

  var map = Maps.newStaticMap().setLanguage('fr')
  .setSize(846,479)
  .setMapType(Maps.StaticMap.Type.HYBRID)
  .setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.GREEN,'1')
  .addMarker(address1)
  .setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'2')
  .addMarker(address2)
  .setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'3')
  .addMarker(address3)
  .setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'4')
  .addMarker(address4)
  .setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'5')
  .addMarker(address5)
  .setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.RED,'6')
  .addMarker(address6)
  .beginPath()
  .addAddress(address1)
  .addAddress(address2)
  .addAddress(address3)
  .addAddress(address4)
  .addAddress(address5)
  .addAddress(address6)
  .endPath()
  .getBlob()
  sheet.insertImage(map,5,15)
}

这将遍历您的所有地址并检查它们是否为空,并仅将具有非空地址的地址添加到地图中:

function onOpen() {
  SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
    .addItem(' Générer les Cartes', 'myFunction')
    .addToUi();
  myFunction()
}

function myFunction() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName('AVRIL 22');
  var addresses = sheet.getRange('K121:K126').getValues().flat();
  var colors = [ Maps.StaticMap.Color.GREEN,Maps.StaticMap.Color.BLUE,
                 Maps.StaticMap.Color.BLUE,Maps.StaticMap.Color.BLUE,
                 Maps.StaticMap.Color.BLUE,Maps.StaticMap.Color.RED];

  var map = Maps.newStaticMap().setLanguage('fr')
  .setSize(846,479)
  .setMapType(Maps.StaticMap.Type.HYBRID);
  
   addresses.forEach((address,i)=>{

     if(address!=''){
          map.addMarker(address)
             .setMarkerStyle(Maps.StaticMap.MarkerSize.MID,colors[i],(i+1).toString())
             .addMarker(address)
     }
   });

   map = map.endPath().getBlob();
   spreadsheet.insertImage(map,5,15)

}

它现在不起作用的另一个原因是因为您的地址无效:

输入正确的值并让我知道代码是否有效。

soMarios 的代码运行良好,但标记颜色不正确。这是另一个社区的解决方案。如果它可以帮助别人,我就让代码。

function onOpen() {
  SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
    .addItem(' Générer les Cartes', 'myFunction')
    .addToUi();
  myFunction()
}

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  //var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Testing")

   // Deletes all images in sheet
    var images = sheet.getImages();
    images.map(function(img){img.remove();});

  var cards = []

  //gets the addresses of each card into an array
  for(i=0; i<16; i++)
  {
    cards.push(sheet.getRange(121, 11 + (15*i), 6, 1).getValues().flat())
  }

  //loop through the array of cards
  for(c=0; c < cards.length; c++){

   //create a new map
   var map = Maps.newStaticMap().setLanguage('fr')
  .setSize(846,479)
  .setMapType(Maps.StaticMap.Type.HYBRID)

  //remove blanks from card addresses
  var card = cards[c].filter(j => j)

  //begin a new path for the map
  map.beginPath()

  //loop through card addresses
  for(n=0; n < card.length; n++){

    //add the nth address to the map
    map.addAddress(card[n])

    //if first address, create new green marker (note n +1 due to array starting from 0 not 1)
    if(n == 0){

      map.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.GREEN,n+1)
      var marker = map.addMarker(card[n])
    }
    //if last address, create new red marker
    else if(n == card.length - 1){
      map.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.RED,n+1)
      var marker = map.addMarker(card[n])
    } 
    //if any other address create blue marker
    else{
      map.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,n+1)
      var marker = map.addMarker(card[n])
    }
  } 
  
  //end the path and insert the map image to the sheet
  map.endPath()
  map.getBlob()
  sheet.insertImage(map,5+ (15*c),15)
  }
}