如何从Google工作表添加标记到谷歌应用程序脚本

时间:2017-11-11 13:20:18

标签: google-maps google-maps-api-3 google-apps-script google-sheets

我在谷歌表中有两列,包含纬度和经度, 我在foramt下面使用:

var map=Maps.newStaticMap().setLanguage('en')
      .beginPath()
      .addMarker(lat, long)
      .addMarker(lat1, long1)
      .addMarker(lat2, long2)
      .addMarker(lat3, long3)
      .endPath().getAs('image/jpeg');

我想从阅读谷歌表中添加多个添加标记,

我在谷歌脚本中使用了这段代码:

var app=SpreadsheetApp
var ss= app.openById("1dNw5loVnxpBysrYC9t49vc5LRkOytSP23Blo-ny_oM");
var sheet2=ss.getSheetByName('Sheet2');
var lastRow2= sheet2.getLastRow();
var numRow=lastRow2-7;
var marker= "";
for (var i=8;i<=lastRow2;i++){
marker=marker+".addMarker("+sheet2.getRange(i,1).getValue()+","+ sheet2.getRange(i,2).getValue()+")";
}
marker=marker+".";

var map=Maps.newStaticMap().setLanguage('en').beginPath()+marker+endPath().getAs('image/jpeg');

标记输出是这样的:

.addMarker(32.645814,51.669745).addMarker(32.646284,51.66835).addMarker(32.645769,51.665775).addMarker(32.645769,51.665775).addMarker(32.645769,51.665775).addMarker(32.645769,51.665775).addMarker(32.647468,51.665443).addMarker(32.650087,51.665625).addMarker(32.652779,51.665893).addMarker(32.653032,51.665389).addMarker(32.653032,51.665389).addMarker(32.653032,51.665389).addMarker(32.653032,51.665389).addMarker(32.653493,51.66599).addMarker(32.653899,51.664413).addMarker(32.654812,51.665571).addMarker(32.656347,51.665582).addMarker(32.656808,51.66202).addMarker(32.656916,51.660207).addMarker(32.660204,51.660572).addMarker(32.663049,51.661119).addMarker(32.663302,51.661666).addMarker(32.663302,51.661666).addMarker(32.663302,51.661666).

当我在地图变量中手动复制过去的标记输出时,它可以工作:

var map=Maps.newStaticMap().setLanguage('en').setMarkerStyle(Maps.StaticMap.MarkerSize.TINY, Maps.StaticMap.Color.RED , '1').beginPath().addMarker(32.645814,51.669745).addMarker(32.646284,51.66835).addMarker(32.645769,51.665775).addMarker(32.645769,51.665775).addMarker(32.645769,51.665775).addMarker(32.645769,51.665775).addMarker(32.647468,51.665443).addMarker(32.650087,51.665625).addMarker(32.652779,51.665893).addMarker(32.653032,51.665389).addMarker(32.653032,51.665389).addMarker(32.653032,51.665389).addMarker(32.653032,51.665389).addMarker(32.653493,51.66599).addMarker(32.653899,51.664413).addMarker(32.654812,51.665571).addMarker(32.656347,51.665582).addMarker(32.656808,51.66202).addMarker(32.656916,51.660207).addMarker(32.660204,51.660572).addMarker(32.663049,51.661119).addMarker(32.663302,51.661666).addMarker(32.663302,51.661666).addMarker(32.663302,51.661666).endPath().getAs('image/jpeg');

当我使用这种形式时,它不起作用:

var map=Maps.newStaticMap().setLanguage('en').beginPath()+marker+endPath().getAs('image/jpeg');

image of my google sheet

2 个答案:

答案 0 :(得分:0)

我无法使用map来添加标记,但是,您可以循环添加标记以进行贴图。这个简单的示例将所有latlong添加为标记。我不知道你用地图做什么。在我的示例中,我获取了url并创建了返回Sheet4的超链接,您可以单击以查看结果。

function multiMarker() {
var map = Maps.newStaticMap()
       .setSize(600, 600)
       .setCenter('32.647468,51.665443');
var ss=SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getSheetByName("Sheet2")
var lr= s.getLastRow();
var rng=s.getRange(2, 1, lr-1, 2).getValues()
for (var i=0;i<=rng.length-1;i++){
    map.addMarker(rng[i][0],rng[i][1])
}
map.getAs('image/jpeg');
   var url=map.getMapUrl();
   var ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet4').getRange("A1").setValue(url);
   var app = UiApp.createApplication().setTitle('No Title');
   app.add(app.createImage(map.getMapUrl()));
    }

答案 1 :(得分:0)

  

你需要+ + +周围的引号。这是您可以运行的测试。检查   记录结果。

谢谢你,艾德尼尔森 但它返回,只有字符串 并且不要返回谷歌地图的图像。