gpt4 book ai didi

javascript - 谷歌应用程序脚本,用于呈现数组值供用户选择

转载 作者:行者123 更新时间:2023-12-03 08:58:38 26 4
gpt4 key购买 nike

我不确定如何编写 GAS 表单按钮来触发具有动态值的脚本。在这种情况下,当前工作表单元格值用于查找相邻工作表中的行并填充结果数组。然后,表单会显示一个按钮列表,其中包含结果数组的一列中的值。按表单按钮应触发脚本 postLocationData,并使用结果数组值更新当前单元格和行中的相邻单元格,然后关闭表单。此时,按下表单按钮似乎没有执行任何操作。预先非常感谢您的帮助:)

function lookUpLocationTest(){
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getActiveCell();
var sheetLocations = ss.getSheetByName('LU_Locations');
var arrayRecords = sheetLocations.getRange(2, 3, sheetLocations.getLastRow(), 2).getValues();


var matchingLocations=[];
for (var i=0;i<arrayRecords.length;i++) {
var result = arrayRecords[i][1].indexOf(cell.getValue())
if(result !== -1) {
matchingLocations.push(arrayRecords[i]);
}
}
if(matchingLocations.length === 0){
var result = ui.alert(
'Message:',
'No Matching Location Found.',
ui.ButtonSet.OK);
return 0;
}
Logger.log(' Process - ' + matchingLocations.length + ' Locations have been found.') ; //matchingLocations is a global

// Prep Form HTML with formatted matching Locations
var HTML= '<form><div>'
for(var i=0;i<matchingLocations.length;i++){
HTML += "<div><input type='button' value='" + matchingLocations[i][1]
+ "' onclick='google.script.run.withSuccessHandler(postLocationData).processForm(this.parentNode)'/></div>";
}
var htmlOutput = HtmlService.createHtmlOutput(HTML).setSandboxMode(HtmlService.SandboxMode.IFRAME);
var result = SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Matching Locations');

return 1;
}

function postLocationData(lookUpValue) {
var location = lookUpValuesInArray (matchingLocations, 1, lookUpValue); //matchingLocations is a global
var cell = currCell;
var latLongCol = 3;
cell.setValue(location[0][1]);
cell.getRowIndex();
var sheet = cell.getSheet();
sheet.getRange(cell.getRowIndex(), latLongCol).setValue(location[0][0]);
var temp =1;
}

最佳答案

函数“google.script.run”将在客户端执行,但它将调用服务器端的函数(您的 .gs 文件)。在这种情况下,您将调用的函数是“processForm()”,您将在其中发送“this.parentNode”作为参数。

在您的 Apps 脚本文件(gs 文件)中,您应该有一个名为“processForm()”的函数,但您没有在示例中发布它。

此函数结束后,如果一切顺利,函数“google.script.run”将执行您在“withSuccessHandler()”中定义的函数。在您的示例中,您使用了“postLocationData”。

该函数将接收 processForm() 执行返回的结果作为参数。

正如我之前提到的,google.script.run 是在客户端调用的,因此如果一切顺利的话将执行的函数(包含在 withSuccessHandler 中的函数)也必须位于客户端。这意味着它必须是 HTML 中包含的脚本的一部分。

按照您发布代码的方式,我会将 onclick 更改为:

onclick='google.script.run.withSuccessHandler(someJavascriptFunction).postLocationData(this.parentNode)

withSuccessHandler 是可选的,如果您决定使用它,那么您应该在 HTML 变量中创建一个 html 脚本标记,该标记具有该 javascript 函数来显示警报或告诉用户单击按钮的结果。

您还可以在appsscript项目中创建一个html文件并像这样调用它:HtmlService.createHtmlOutputFromFile('Index').setSandboxMode(HtmlService.SandboxMode.IFRAME);

这样您就可以获得更清晰的 html 文件和与其关联的 javascript。

希望这有帮助。

关于javascript - 谷歌应用程序脚本,用于呈现数组值供用户选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com