【发布时间】:2017-10-24 19:11:02
【问题描述】:
我是 Google Apps Script 和 Java 的新手,从 MS Office VBA 迁移而来。我已经做了一些研究,但在这方面仍然需要帮助。
我已经设置了两个表格来控制我公司的设备移动。当您将设备从仓库中取出时,其中一个必须填写,另一个必须在返回时填写。因此,我设置了一个 onFormSubmit 触发器来切换特定电子表格上的每个设备状态,以便对获取和返回的内容有一个概览。
但是,我找不到使用触发器函数上的“e”参数执行此操作的方法。我花了一些时间试图解决这个问题,但我确实想出了一个不太好的解决方案:查看带有表单响应的电子表格,阅读最后一行并将信息分解以做我需要的事情。最大的问题是我通过这种方式发现了一些不稳定性,因为有时带有响应的电子表格需要一些时间来更新。无论如何,这是我现在的“设备移除表格”的代码:
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”访问答案,而不是访问响应电子表格?任何帮助都会很棒!
【问题讨论】:
标签: google-apps-script google-sheets google-apps google-forms