我嘗試獲取代碼并使其適應我的需求,但沒有成功,因為我是腳本新手。
此代碼允許生成帶有每個地址的標記和連接它們的線的谷歌地圖影像。起初,此代碼用于我們必須在代碼中指明的 2 個地址,并且生成的影像是通過電子郵件發送的。
所以我重寫了代碼(我的第一個)來繪制和標記 6 個地址的點,然后將生成的影像粘貼到我作業表的一個單元格中。
我遇到的問題是,當至少一個包含地址的單元格不包含值(因此該值是地址)時,我收到錯誤“例外:無效引數:地址”。我需要在 .getValue() 函式之后添加一些東西,但我不能。
我知道這個問題的答案對于高級腳本用戶來說可能看起來很簡單,但這段代碼可能對其他人有用。
感謝您的洞察力和放縱。
我的床單:

輸入正確的值,讓我知道代碼是否有效。
uj5u.com熱心網友回復:
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)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/470150.html
