简体   繁体   English

根据Google Apps脚本(电子表格)中的表单输入定义变量

[英]Define variable based on form input in Google Apps Script (Spreadsheet)

I want to have a form that allows the user to input some data, that then gets stored in variables for use in other functions. 我想要一种允许用户输入一些数据的表格,然后将其存储在变量中以供其他功能使用。 Specifically, I just need them to be able to define the name of two sheets (shtName and databaseName). 具体来说,我只需要它们能够定义两张纸的名称(shtName和databaseName)。

I can't get the variable to work in the script though as it tells me the variables are undefined (referring to shtName & databaseName here). 我无法使变量在脚本中工作,因为它告诉我变量未定义(在此引用shtName和databaseName)。

I get this error: ReferenceError: "shtName" is not defined. 我收到此错误: ReferenceError: "shtName" is not defined.

If I define shtName and databaseName outside a function, it works. 如果我在函数外部定义shtName和databaseName,那么它将起作用。 So that's obviously the problem but not sure how to get around it. 因此,这显然是问题所在,但不确定如何解决。

index.html 的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 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
}

EDIT: 编辑:

Ok, one way of doing it is by adding this inside function findingtags3() 好的,一种实现方法是在函数内部添加findtags3()

var shtName = Browser.inputBox("Current sheet name");
var databaseName = Browser.inputBox("Input sheet name of database you want to search");

I suppose that will do, but it would be nicer to have a proper setup where you enter that information and it gets stored, until you change the input. 我想这样做可以,但是最好有一个适当的设置来输入信息并存储它,直到您更改输入为止。

You can use propertiesService to store you variable until you are ready to execute the function. 您可以使用propertiesService来存储变量,直到准备执行该函数为止。

Your code will look like this: 您的代码将如下所示:

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
}

This is document specific property if you want this values to be unique for each user. 如果希望每个用户的值唯一,则这是文档特定的属性。 Use user properties instead. 请改用用户属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM