从表单响应中收集字符串对象

Collect string objects from Form response

我是 Google Apps Script 和 Java 的新手,正在从 MS Office VBA 迁移过来。我已经做了一些研究,但在这方面仍然需要帮助。

为了控制我公司的设备移动,我设置了两个表格。其中一个必须在您从存储中取出设备时填写,另一个必须在 return 上填写。因此,我设置了一个 onFormSubmit 触发器来切换特定电子表格上的每个设备状态,以便大致了解所采取的内容和 returned.

但是,我找不到使用触发器函数上的 'e' 参数来执行此操作的方法。我花了一些时间尝试解决这个问题,但我确实想出了一个不太好的解决方案:查看带有表单响应的电子表格,阅读最后一行并将信息分解开来做我需要的。最大的问题是我通过这种方式发现了一些不稳定性,因为有时带有响应的电子表格需要一些时间来更新。无论如何,这是我现在为 "Equipment removal Form":

准备的代码
function onFormSubmit(e) {

  Utilities.sleep(30000); // I put this to try to minimize the Spreadsheet update problem

  var ds = SpreadsheetApp.openById("storage-control-sheet-id").getSheetByName("Control");
  var os = SpreadsheetApp.openById("form-responses-sheet-id").getSheetByName("Form Responses 1");
  var val = os.getRange(os.getLastRow(), 10, 1, 14).getValues().toString(); // holds data from region with the equipments names
  var responsavel = os.getRange(os.getLastRow(), 4).getValue().toString(); // holds the name of the person in charge of equipment
  var motivo = os.getRange(os.getLastRow(), 3).getValue().toString(); // holds the reason for using the equipment
  var prevret = os.getRange(os.getLastRow(), 6).getValue(); // holds the date for returning the equipment
  var table = ds.getRange("B3:B149").getValues(); // holds a list with equipment names
  var output = ds.getRange("C3:F149").getValues(); // holds current info at the Storage Control Spreadsheet
  // now working with the data collected...
  var arr = [];
  var indexes = []; // collect the lines that need to change on the ds object
  val = val.split(",");
  for (var i = 0; i<val.length; i++){
    if (val[i].toString()!=="") {
      arr.push(val[i].trim());
    }
  }

  for (var i = 0; i<table.length;i++){
    if (arr.indexOf(table[i][0]) >= 0) {
      indexes.push(i); //checking which lines on 'output' should change 
    }
  }
  for (var i = 0; i<indexes.length; i++) {
    output[indexes[i]][0] = "In use";
    output[indexes[i]][1] = responsavel;
    output[indexes[i]][2] = motivo;
    output[indexes[i]][3] = prevret;
  }

  ds.getRange("C3:F149").setValues(output);
  SpreadsheetApp.flush();
}

从我在这里看到的情况来看,如果我可以从事件参数 'e' 中获取包含某些问题的答案的单个字符串对象,并将其存储在 'val' 变量中,那将做它。如何直接从 'e' 访问答案,而不是转到响应电子表格?任何帮助都会很棒!

您可以通过引用事件的 "values" 或 "namedValues" 属性直接从事件中获取值,这些属性记录在此处:https://developers.google.com/apps-script/guides/triggers/events#form-submit

   e.values
   e.namedValues

namedValues 是一个将表单字段名称映射到响应值的对象。

values 是一个数组,其中包含按照它们在电子表格中出现的顺序排列的响应。