【问题标题】:Collect string objects from Form response从表单响应中收集字符串对象
【发布时间】: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


    【解决方案1】:

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

       e.values
       e.namedValues
    

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

    values 是一个数组,其中包含的响应与它们在电子表格中出现的顺序相同。

    【讨论】:

    • 我不知道会发生什么,但我只是将Logger.log(e.values); 放在第一个代码行中以查看其中的内容,但它未定义。发生这种情况的任何具体原因?
    • 如果你手动运行它,e总是未定义,否则我不确定,我知道这个功能通常可以工作,因为我有几个基于它的项目
    • 我知道...当我提交表单时会发生这种情况。
    • Logger.log(e) 时有什么提示吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多