简体   繁体   中英

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).

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).

I get this error: ReferenceError: "shtName" is not defined.

If I define shtName and databaseName outside a function, it works. So that's obviously the problem but not sure how to get around it.

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
}

EDIT:

Ok, one way of doing it is by adding this inside function findingtags3()

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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