我想要一个允许用户输入一些数据的表单,然后将其存储在变量中以便在其他函数中使用。具体来说,我只需要它们就能定义两个表的名称(shtName和databaseName)。
我无法让变量在脚本中工作,因为它告诉我变量是未定义的(在这里指的是shtName& databaseName)。
我收到此错误:ReferenceError: "shtName" is not defined.
如果我在函数外部定义shtName和databaseName,它就可以工作。所以这显然是问题,但不确定如何绕过它。
的index.html
<html>
<head>
<base target="_top">
</head>
<body>
<form name="myForm">
Name of current sheet:<br>
<input type="text" name="formshtname" >
<br><br>
Which database sheet to search in?<br>
<input type="text" name="formtagdatabase">
<br><br>
<input type="button" value="Submit"
onclick="google.script.run
.withSuccessHandler(google.script.host.close)
.formSubmit(this.parentNode)" />
<input type="button" value="Close" onclick="google.script.host.close()" />
</form>
</body>
</html>
addTags.gs
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Tags')
.addItem('Setup', 'openSetup')
.addSeparator()
.addItem('Add tags', 'findingTags3')
.addToUi();
}
function openSetup() {
var html = HtmlService.createHtmlOutputFromFile('index').setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showModalDialog(html, 'To auto complete tags, fill in the form');
}
function formSubmit(form) {
var shtName = form.formshtname;
var databaseName = form.formtagdatabase;
}
function findingTags3() {
// bunch of code
/* current spreadsheet */
var spreadsheet = SpreadsheetApp.getActive()
var sht = SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName(shtName)); // want this to be defined from form
/* tag database spreadsheet */
var spreadsheet2 = SpreadsheetApp.openById("xxx");
var sht2 = spreadsheet2.getSheetByName(databaseName); // want this to be defined from form
// more code
}
编辑:
好的,这样做的一种方法是在函数内添加findtags3()
var shtName = Browser.inputBox("Current sheet name");
var databaseName = Browser.inputBox("Input sheet name of database you want to search");
我想会这样做,但如果您输入该信息并进行存储,那么进行正确设置会更好。直到您更改输入。
答案 0 :(得分:0)
您可以使用propertiesService存储变量,直到您准备好执行该函数。
您的代码如下所示:
function exampleSubmit(){ // this function will set a mock/default values to properties
var form = {
"formshtname" : "Sheet1",
"formtagdatabase" : "Sheet4"
}
Logger.log(form)
formSubmit(form)
}
function formSubmit(form) {
var docProp = PropertiesService.getDocumentProperties()
docProp.setProperty("shtname", form.formshtname)
docProp.setProperty("databaseName",form.formtagdatabase)
}
function findingTags3() {
// bunch of code
var docProp = PropertiesService.getDocumentProperties()
var shtName = docProp.getProperty("shtname")
var databaseName = docProp.getProperty("databaseName")
/* current spreadsheet */
var spreadsheet = SpreadsheetApp.getActive()
var sht = SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName(shtName)); // want this to be defined from form
Logger.log(sht.getName())
/* tag database spreadsheet */
var spreadsheet2 = SpreadsheetApp.openById("xxx");
var sht2 = spreadsheet2.getSheetByName(databaseName); // want this to be defined from form
Logger.log(sht2.getName())
// more code
}
如果您希望此值对每个用户都是唯一的,则这是特定于文档的属性。请改用user properties。