脚本 Google 表格以生成 Google 地图图片并将其粘贴到单元格中
Script Google Sheets to generate Google Maps picture and paste it into a cell
我尝试获取代码并根据我的需要进行调整,但没有成功,因为我是脚本新手。
此代码允许生成带有多个地址标记的 google 地图,然后通过电子邮件发送。在原始代码中,需要指明地点或地址。我希望从选项卡的不同单元格中检索地址。在我的代码中,有问题的单元格在引号之间并以字母 K 开头。然后我希望将地图插入合并的单元格 E15:M37 或只是选项卡的 E15 而不是通过电子邮件接收它。
我还在代码中指明了选项卡的名称,但我希望 onOpen() 函数仅在活动选项卡中运行。
我的表格:
我的代码:
function onOpen() {
SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
.addItem(' Générer les Cartes', 'myFunction')
.addToUi();
myFunction()
}
function myFunction() {
const ss = SpreadsheetApp.getActive();
const map = Maps.newStaticMap()
.setSize(882,500)
.setMapType(Maps.StaticMap.Type.SATELLITE)
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.GREEN,'1')
.addMarker("K121")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'2')
.addMarker("K122")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'3')
.addMarker("K123")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'4')
.addMarker("K124")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'5')
.addMarker("K125")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.RED,'6')
.addMarker("K126")
.setPathStyle(3, Maps.StaticMap.Color.BLACK, Maps.StaticMap.Color.BLACK)
.beginPath()
.addAddress("K121")
.addAddress("K122")
.addAddress("K123")
.addAddress("K124")
.addAddress("K125")
.addAddress("K126")
.endPath()
.getBlob()
GmailApp.sendEmail('mozilla075@gmail.com', 'Carte Groupe Ghena', '', {
attachments: [map],
})
}
我修改了代码并且它可以工作,但只有当包含地址的单元格不为空时。只是碰巧它们是空的,所以脚本没有执行。
修改后的代码如下:
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 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)
}
}
我尝试获取代码并根据我的需要进行调整,但没有成功,因为我是脚本新手。
此代码允许生成带有多个地址标记的 google 地图,然后通过电子邮件发送。在原始代码中,需要指明地点或地址。我希望从选项卡的不同单元格中检索地址。在我的代码中,有问题的单元格在引号之间并以字母 K 开头。然后我希望将地图插入合并的单元格 E15:M37 或只是选项卡的 E15 而不是通过电子邮件接收它。
我还在代码中指明了选项卡的名称,但我希望 onOpen() 函数仅在活动选项卡中运行。
我的表格:
我的代码:
function onOpen() {
SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
.addItem(' Générer les Cartes', 'myFunction')
.addToUi();
myFunction()
}
function myFunction() {
const ss = SpreadsheetApp.getActive();
const map = Maps.newStaticMap()
.setSize(882,500)
.setMapType(Maps.StaticMap.Type.SATELLITE)
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.GREEN,'1')
.addMarker("K121")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'2')
.addMarker("K122")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'3')
.addMarker("K123")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'4')
.addMarker("K124")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLUE,'5')
.addMarker("K125")
.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.RED,'6')
.addMarker("K126")
.setPathStyle(3, Maps.StaticMap.Color.BLACK, Maps.StaticMap.Color.BLACK)
.beginPath()
.addAddress("K121")
.addAddress("K122")
.addAddress("K123")
.addAddress("K124")
.addAddress("K125")
.addAddress("K126")
.endPath()
.getBlob()
GmailApp.sendEmail('mozilla075@gmail.com', 'Carte Groupe Ghena', '', {
attachments: [map],
})
}
我修改了代码并且它可以工作,但只有当包含地址的单元格不为空时。只是碰巧它们是空的,所以脚本没有执行。
修改后的代码如下:
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 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)
}
}