如何根据 Google 表单下拉值更新字段?

How do I update fields based on a Google Forms dropdown value?

我们正在使用 Google 表格从我们的现场代表那里收集数据,而他们在工作现场。我们使用一个脚本来填充 4 个不同字段的下拉值并定期运行。这避免了他们必须输入并可能犯错误。

所有这些字段都是预先确定的,因此如果它们 select 项目 #1,我们已经知道地址、经理和客户的值。这些值存储在结果表单的第二个选项卡中,理论上可以用作查找。

我质疑是否需要包含这些字段,但是当用户提交表单时,数据随后会使用附加组件通过电子邮件发送,并在主题中使用其中的一些字段。我想收件人也想要这些信息。

我可以制作自己的触发器吗?如果我有项目 ID 更改时间的触发器,我想我可以从电子表格更新其他人。

或者如果有一个 OnNextPage 触发器我可以用来填充这些值。

提交表单时是否有 add/change 值?

我认为以下是我想要做的(感谢@OMilo),但我正在努力查找:

函数 onFormSubmit(e) { Logger.log([方法] onFormSubmit");

发送邮件(e.range); }

函数发送电子邮件(范围){

// 这里我们选择用户 selected 的形式: var itemResponse = e.response.getItemResponses()[1557784536]; // 使用项目的任何索引,在我的例子中是 0。 var kmbProjectNumber = itemResponse.getResponse();

//LOOKUP KMB PROJECT NUMBER IN SHEET: "New - KMB Design Visit Closeout (Responses)" TAB: "KMB Lookups"
var ss = SpreadsheetApp.OpenByID('REDACTED');
var lookups = ss.getSheetByName("KMB Lookups");

//In Column named'KMB Project ID' find record from bottom where value = kmbProjectNumber
//Get value for column named 'Site Address'
//Get value for column named 'KMB Project Manager'
//Get value for column named 'Client Assigning this Work'

// FETCH SPREADSHEET //
var values = range.getValues();
var row = values[0];

//WRITE FOUND VALUES TO SPREADSHEET
//Set row[3] to Site Address
//Set row[4] to Project Manger
//Set row[5] to Client

var svProjectManager = row[4]; //Update manually  //E
var svClient = row[5]; //Update manually  //F

// EXTRACT VALUES //
var svTimeStamp = row[0];  //A
var svInitials = row[1];  //B
var svProjectID = row[2];  //C
var svSiteAddress = row[3];  //Update manually    //D
var svProjectManager = row[4]; //Update manually  //E
var svClient = row[5]; //Update manually  //F
var svNOCNeeded = row[6];  //G


// PREPARE EMAIL //
var emailRecipients = "REDACTED";
var emailSubject = "Field Visit Completed for "+svProjectManager+" "+svProjectID;
var emailBody = ""  //Format using variables set

// SEND EMAIL //

}

您不需要在表单本身中包含这些字段。相反,您可以根据他们在 onFormSubmit 触发器触发的函数中选择的选项设置不同的值。

我写了一个小示例来展示如何做到这一点。

首先,如果您还没有创建 onFormSubmit 触发器:

function createTrigger() {
  var form = FormApp.openById('your_form_id'); // Change accordingly
  ScriptApp.newTrigger('getData')
    .forForm(form)
    .onFormSubmit()
    .create();
}

接下来,创建触发器将在用户提交表单时调用的函数:

function getData(e) {
  // Here we get choice the users selected in the form:
  var itemResponse = e.response.getItemResponses()[0]; // Use whatever index is the item, in my case it's 0.
  var response = itemResponse.getResponse();
  var values = getDataFromSheet(); // Getting the values from the sheet
  for(var i = 1; i < values.length; i++) { // Checking which row from the sheet corresponds to the choice selected in the form
    var row = values[i];
    if(row[2] == response) { // Checks if current sheet row is the one selected in the form submission
      var emailData = { // Data for email to be sent is assigned depending on form choice and sheet data
        project: row[2],
        address: row[3],
        manager: row[4],
        client: row[5]
      }
    }
  }
  // Preparing email parameters
  var emailBody = emailData["address"] + " - " + emailData["manager"] + " - " + emailData["client"];
  var emailSubject = "Field Visit Completed for " + emailData["manager"] + " " + emailData["project"];
  var emailRecipient = 'valid email'; // Change accordingly
  // Sending email
  MailApp.sendEmail(emailRecipient, emailSubject, emailBody);
}

在上面的函数中,调用了getDataFromSheet

function getDataFromSheet() {
  var ss = SpreadsheetApp.openById('your_spreadsheet_id'); // Change accordingly
  var lookups = ss.getSheetByName("KMB Lookups");
  var values = lookups.getDataRange().getValues();
  return values;
}

希望对你有用。