[英]How to turn form responses object into a 2D array with the 1st row being the key/header and the 2nd the values/answers using GAS?
Here's how the incoming data arrives:以下是传入数据的到达方式:
let e = {"authMode":"FULL","namedValues":{"Question 1":["Answer question 1"],"Question 2":["Answer question 2"],"Question 3.":["Answer Question 3"]}}
This should look like this:这应该是这样的:
[
["Question 1", "Question 2", "Question 3"],
["Answer question 1", "Answer question 2", "Answer question 3"]
]
I'll be working on getting the headers to match the destination sheet column headers and, therefore, the object above would need to be converted to a similar format.我将努力使标题与目标工作表列标题相匹配,因此,上面的 object 需要转换为类似的格式。
How can this be done using Google Apps Script?如何使用 Google Apps 脚本完成此操作? I've seen some example, but none found on this one precisely.
我见过一些例子,但在这个例子中没有找到。
Thank you!谢谢!
Is this something like that?是这样的吗?
const e = { "authMode": "FULL", "namedValues": { "Question 1": ["Answer question 1"], "Question 2": ["Answer question 2"], "Question 3.": ["Answer Question 3"] } }
const res = Object.entries(e.namedValues).reduce((acc, [questionNumber, answer]) => {
acc[0].push(questionNumber)
// spread answer because answer is an array
acc[1].push(...answer)
return acc
}, [[], []])
or a more compact way less comprehensible maybe或者更紧凑的方式不太容易理解
const res = Object.entries(e.namedValues).reduce((acc, [questionNumber, answer]) => [[...acc[0], questionNumber],[...acc[1], ...answer]] ,[[],[]]))
Editing the form often cause the number of available answers to a given question to grow.编辑表单通常会导致给定问题的可用答案数量增加。
function onMyForm(e) {
const arr = Object.keys(e.namedValues).reduce((a,c) => {
if(c && c.length > 0 && !a.hasOwnProperty(c)) {
a[1].push(e.namedValues[c][e.namedValues[c].length - 1]);
a[0].push(c);
}
return a;
},[[],[]])
return arr;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.