![](/img/trans.png)
[英]Run a script in Google Sheets that generates formulas into cells when a specific cell is not empty
[英]Script Google Sheets error because cell is empty
我嘗試獲取代碼並根據我的需要進行調整,但沒有成功,因為我是腳本編寫方面的新手。
這段代碼允許生成一張谷歌地圖的圖像,每個地址都有標記和一條連接它們的線。 起初這段代碼用於我們必須在代碼中指明的 2 個地址,生成的圖像由 email 發送。
所以我將代碼(我的第一個)重寫為 plot 並標記 6 個地址的點,然后將生成的圖像粘貼到我的工作表的一個單元格中。
我遇到的問題是,當至少一個包含地址的單元格不包含值(因此該值是一個地址)時,我收到錯誤“異常:無效參數:地址”。 我需要在 the.getValue() function 之后添加一些內容,但我不能。
我知道這個問題的答案對於高級腳本用戶來說可能看起來很簡單,但也知道這段代碼可能對其他人有用。
感謝您的洞察和寬容。
我的床單:
我的劇本:
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)
}
這將循環遍歷所有地址並檢查它們是否為空,並僅將具有非空地址的地址添加到 map:
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 的代碼運行良好,但標記 colors 不正確。 這是另一個社區的解決方案。 如果它可以幫助別人,我就讓代碼。
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)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.