[英]How to collect data from form entries when editing a previously submitted form using Google Apps Script?
I have a Google form that allows users to edit a previously submitted response.我有一个谷歌表单,允许用户编辑以前提交的回复。 When a form is submitted the first time, I capture the timestamp, serial # and tech as variables and push them to a slack channel as a chat notification along with writing all of the responses to a spreadsheet.
第一次提交表单时,我将时间戳、序列号和技术捕获为变量,并将它们作为聊天通知推送到松弛通道,同时将所有响应写入电子表格。 I am using an onFormSubmit trigger for the function.
我正在为 function 使用 onFormSubmit 触发器。 This part works great.
这部分效果很好。
There are fields that are not required, but are filled out later by editing the form responses.有些字段不是必需的,但稍后通过编辑表单响应来填写。 When the form is edited and submitted, the serial # and tech fields are not changed.
编辑和提交表单时,序列号和技术字段不会更改。 The spreadsheet is updated accordingly, but for the slack channel notification, the only variable that has data is the timestamp.
电子表格会相应更新,但对于松弛通道通知,唯一具有数据的变量是时间戳。 The serial and tech are blank due to those fields not changing on the form.
由于表单上的这些字段未更改,因此序列号和技术为空白。 How do I create a variable for serial and tech using the pre-existing form response for my slack notification?
如何使用我的松弛通知的预先存在的表单响应为序列和技术创建变量?
My current code checks if the tech value is blank or not and if not blank it sends a slack notification.我当前的代码检查技术值是否为空白,如果不是空白,它会发送一个松弛通知。 If it is blank, it does nothing.
如果它是空白的,它什么也不做。 If it is blank, I want it to capture the existing form responses as an array that I can use the values to create a slack notification showing the same data, but include that the original form has been re-submitted with the pre-existing values.
如果它是空白的,我希望它将现有的表单响应捕获为一个数组,我可以使用这些值来创建一个显示相同数据的松弛通知,但包括原始表单已使用预先存在的值重新提交.
I am new to coding and have been researching and testing for over a week with no results.我是编码新手,并且已经研究和测试了一个多星期,但没有任何结果。
var timestamp = 'Timestamp' //type exact column header as shown in workbook
var tech = 'Troubleshooting Tech' //type exact column header as shown in workbook
var parts = 'Part(s)# Ordered' //type exact column header as shown in workbook
var serial = 'Serial #' //type exact column header as shown in workbook
var slackWebHook = '' //Paste Slack webhook here
function SendSlackMessageEvent(e) {
const ss = SpreadsheetApp.getActive();
let data = e.namedValues
let payload = buildAlert(data);
if (data[tech] == '') {
}
if (data[tech] != '') {
sendAlert(payload);
}
}
function buildAlert(data) {
let timestampe = data[timestamp];
let teche = data[tech];
let seriale = data[serial];
let partse = data[parts];
let payload = {
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":bell: *Breakfix Ticket Submitted* :bell:"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": teche + " submitted a ticket on " + timestampe
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Serial #: " + seriale
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Parts Needed: " + partse
}
}
]
};
return payload;
}
function sendAlert(payload) {
// const webhook = ""; //Paste your webhook URL here
const webhook = slackWebHook; //slack webhook URL
var options = {
"method": "post",
"contentType": "application/json",
"muteHttpExceptions": true,
"payload": JSON.stringify(payload)
};
try {
UrlFetchApp.fetch(webhook, options);
} catch(e) {
Logger.log(e);
}
}
The problem in your code is that you are assuming that an unchanged response will be included in the namedValues
properties as an empty string but it will not be included so:您的代码中的问题是您假设未更改的响应将作为空字符串包含在
namedValues
属性中,但不会包含在内:
data[tech]
will return undefined
data[tech]
将返回undefined
Instead of代替
if (data[tech] == '') {
}
if (data[tech] != '') {
sendAlert(payload);
}
you might use你可能会使用
if(data[tech]){
// add here what should be done when data[tech] has a value
} else {
// add here what should be done when data[tech] is undefined
}
By the other hand, since the form submit event object includes a range property having the response corresponding range you could use it instead of namedValues property:另一方面,由于表单提交事件 object 包含一个具有响应对应范围的范围属性,您可以使用它而不是 namedValues 属性:
e.range.getValues()
return Array holding a Array with the values logged in the spreadsheet. e.range.getValues()
返回 Array,其中包含一个 Array,其中记录了电子表格中的值。
Related有关的
Reference参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.