如何在 DoGet() 函数中读取表单的内容?

How can I read the contents of a form within a DoGet() function?

我正在做一个map painter programme, that takes in X, Y and colour data from user input and stores in a Google spreadsheet

数据以表格形式发送到传播sheet,紧随其后solution。提交表单后,它会作为新的 row 发送到名为 Globalsheet。但是,我有三个 sheet、GlobalLocalDungeon,并且根据提交的表单数据中包含的变量,我想要 doGet() 函数我的应用程序sheets 将新行发送到正确的 sheet。

这是 appScript DoGet():

  function doGet (e) {

  const lock = LockService.getScriptLock()
  lock.tryLock(10000)

  try {
    const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))

    //++++++++++++++++++SEND TO GLOBAL++++++++++++++++++++++

    const Global = doc.getSheetByName("Global")
   

    const headers = Global.getRange(1, 1, 1, Global.getLastColumn()).getValues()[0]
    const nextRow = Global.getLastRow() + 1

    const newRow = headers.map(function(header) {
      //returns a new array, produced by applying a fucntion to every value in an array 
    return header === 'UniqueID' ? new Date() : e.parameter[header]
    })


    Global.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])

    //++++++++++++++++++++SEND TO LOCAL++++++++++++++++++++

  
  }

澄清一下,这段代码有效,但我正在努力了解如何告诉它 sheet(Global LocalDungeon)也发送新行.现在,他们都将走向全球。但是,如果我的表单中有一个字段显示“全球”、“本地”或“地下城”,我如何才能让应用脚本读取它并采取相应的行动?

从概念上讲,我需要引用传入表单数组中包含 sheet 名称的部分,然后我可以编写脚本以将其用作 [=37= 的名称] 像这样:

const sheetname = doc.getSheetByName("Global/Local/Dungeon")

您可以使用 google.script.run 来触发脚本中的函数并传递值。请参阅这个经过大修的脚本。

示例表格:

脚本:

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('Index');
}

function processData(data) {
  const lock = LockService.getScriptLock();
  lock.tryLock(10000);
  try {
    const doc = SpreadsheetApp.openById(ScriptProperties.getProperty('key'));
    const sheet = doc.getSheetByName(data['sheet']);

    const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    const nextRow = sheet.getLastRow() + 1;

    const newRow = headers.map(function(header) {
      return header === 'timestamp' ? new Date() : data[header];
    });

    sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow]);
  } catch(e) {    
  } finally {
    lock.releaseLock();
  }
}

Index.html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form name="submit-to-google-sheet">
      <label for="sheet">Sheet:</label><br>
      <select id="sheet" name="sheet">
        <option value="Local">Local</option>
        <option value="Global">Global</option>
        <option value="Dungeon">Dungeon</option>
      </select>
      <input name="email" type="email" placeholder="Email" required>
      <input name="firstName" type="text" placeholder="First Name">
      <input name="lastName" type="text" placeholder="Last Name">
      <button type="submit" onclick="google.script.run.processData(this.form);">Send</button>
    </form> 
  </body>
</html>

输入:

输出:

Octavia,谢谢你的详细证明。然而,我的问题的答案是:

const sheet = doc.getSheetByName(e.parameter[sheetName])

我可以使用语法 e.parameter[<column header>]

来引用传入的表单数据