Google 脚本映射
Google Script Mapping
我试图在 Google 脚本上使用映射在城市 (id="city") 字段中输入一个值,并使用下面的脚本显示价格 (id="price")。下面的代码不起作用,也没有在价格文本框中提供任何结果。此外,是否可以做多个条件?我的意思是价格将根据城市、产品和数量显示。我提供了下面的图片。谢谢!
<script>
document.getElementById("btn").addEventListener("click",getPrice);
function getPrice(){
vCity = document.getElementById("city").value;
google.script.run.withSuccessHander(updatePrice).getAmount(vCity);
}
function updatePrice(myprice) {
document.getElementById("price").value = myprice;
}
</script>
这是我的 gs。
function doGet(e) {
return HtmlService.createHtmlOutputFromFile("page");
}
function getAmount(city) {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1FhWlaXixXPxq8EnHICfVR207aIkxtkzOgvdvt3hHuuI/edit#gid=0");
var ws = ss.getSheetByName("Pricing");
var data = ws.getRange(1, 1, ws.getLastRow(),2).getValues();
var cityList = data.map(function(r){ return r[0]; });
var priceList = data.map(function(r){ return r[1]; });
var position = cityList.indexOf(city);
if(position > -1){
return priceList[position];
} else {
return "Unavailable";
}
这对我有用。我 运行 它作为一个对话框。我更改了工作表名称。
function launchmypricedialog() {
let html = '<input type="text" id="city" placeholder="City" /><br /><input type="text" id="price" /><br /><input type="button" value="Click Me" id="btn"/><script>document.getElementById("btn").addEventListener("click",getPrice);function getPrice(){let vCity = document.getElementById("city").value;google.script.run.withSuccessHandler(updatePrice).getAmount(vCity);}function updatePrice(myprice) {console.log(myprice);document.getElementById("price").value = myprice;}</script>';
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'test');
}
function getAmount(city) {
var ss = SpreadsheetApp.getActive();
var ws = ss.getSheetByName("Sheet1");
var data = ws.getRange(2, 1, ws.getLastRow()-1, 2).getValues();
var cityList = data.map(function (r) { return r[0]; });
var priceList = data.map(function (r) { return r[1]; });
//console.log(city);
//console.log(JSON.stringify(cityList));
//console.log(JSON.stringify(priceList));
var position = cityList.indexOf(city);
//console.log(priceList[position]);
if (position > -1) {
return priceList[position];
} else {
return "Unavailable";
}
}
这是我的数据
COL1
COL2
City1
12
City2
2
City3
24
City4
18
City5
14
City6
3
City7
17
City8
5
City9
29
City10
24
City11
8
City12
26
City13
15
City14
25
City15
0
City16
25
City17
17
City18
11
City19
22
City20
10
City21
16
City22
0
City23
6
City24
22
City25
15
City26
11
City27
13
City28
14
City29
22
City30
24
我的对话:
我试图在 Google 脚本上使用映射在城市 (id="city") 字段中输入一个值,并使用下面的脚本显示价格 (id="price")。下面的代码不起作用,也没有在价格文本框中提供任何结果。此外,是否可以做多个条件?我的意思是价格将根据城市、产品和数量显示。我提供了下面的图片。谢谢!
<script>
document.getElementById("btn").addEventListener("click",getPrice);
function getPrice(){
vCity = document.getElementById("city").value;
google.script.run.withSuccessHander(updatePrice).getAmount(vCity);
}
function updatePrice(myprice) {
document.getElementById("price").value = myprice;
}
</script>
这是我的 gs。
function doGet(e) {
return HtmlService.createHtmlOutputFromFile("page");
}
function getAmount(city) {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1FhWlaXixXPxq8EnHICfVR207aIkxtkzOgvdvt3hHuuI/edit#gid=0");
var ws = ss.getSheetByName("Pricing");
var data = ws.getRange(1, 1, ws.getLastRow(),2).getValues();
var cityList = data.map(function(r){ return r[0]; });
var priceList = data.map(function(r){ return r[1]; });
var position = cityList.indexOf(city);
if(position > -1){
return priceList[position];
} else {
return "Unavailable";
}
这对我有用。我 运行 它作为一个对话框。我更改了工作表名称。
function launchmypricedialog() {
let html = '<input type="text" id="city" placeholder="City" /><br /><input type="text" id="price" /><br /><input type="button" value="Click Me" id="btn"/><script>document.getElementById("btn").addEventListener("click",getPrice);function getPrice(){let vCity = document.getElementById("city").value;google.script.run.withSuccessHandler(updatePrice).getAmount(vCity);}function updatePrice(myprice) {console.log(myprice);document.getElementById("price").value = myprice;}</script>';
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'test');
}
function getAmount(city) {
var ss = SpreadsheetApp.getActive();
var ws = ss.getSheetByName("Sheet1");
var data = ws.getRange(2, 1, ws.getLastRow()-1, 2).getValues();
var cityList = data.map(function (r) { return r[0]; });
var priceList = data.map(function (r) { return r[1]; });
//console.log(city);
//console.log(JSON.stringify(cityList));
//console.log(JSON.stringify(priceList));
var position = cityList.indexOf(city);
//console.log(priceList[position]);
if (position > -1) {
return priceList[position];
} else {
return "Unavailable";
}
}
这是我的数据
COL1 | COL2 |
---|---|
City1 | 12 |
City2 | 2 |
City3 | 24 |
City4 | 18 |
City5 | 14 |
City6 | 3 |
City7 | 17 |
City8 | 5 |
City9 | 29 |
City10 | 24 |
City11 | 8 |
City12 | 26 |
City13 | 15 |
City14 | 25 |
City15 | 0 |
City16 | 25 |
City17 | 17 |
City18 | 11 |
City19 | 22 |
City20 | 10 |
City21 | 16 |
City22 | 0 |
City23 | 6 |
City24 | 22 |
City25 | 15 |
City26 | 11 |
City27 | 13 |
City28 | 14 |
City29 | 22 |
City30 | 24 |
我的对话: