简体   繁体   English

如何使用 GAS 将表单响应 object 转换为二维数组,其中第一行是键/标题,第二行是值/答案?

[英]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]] ,[[],[]]))

This will work even if you have edited the form即使您已经编辑了表单,这也会起作用

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.

相关问题 如何通过使用Javascript或JQuery对第一个数组进行排序来对第二个数组进行排序? - How to sort 2nd array by sorting the 1st one using Javascript or JQuery? 通过使用第二个数组(JavaScript)中的引用从第一个数组中的每个 object 中提取每个值来创建一个新数组 - Make a new array by extracting each value from each object in the 1st array using the references in the 2nd array (JavaScript) Javascript(Adobe Acrobat XI)从表单第一页输入的答案中填充第二页表单 - Javascript (Adobe Acrobat XI) populate 2nd page form from inputted answers from 1st page in form 第一个单词第一个字符第二个单词第二个字符直到数组结尾 - 1st word 1st character 2nd word 2nd character and on till the end of array 检查数组中的匹配键,如果找到,则取第二个数组条目,如果在 javascript 中不取第一个数组条目 - Check for matching key in array, if found take 2nd array entries if not take 1st array entries in javascript 如何将两个父对象继承到一个子对象,但是第二个继承对象的参数是第一个继承对象 - How to inherit two parent object to a child object, but the argument of the 2nd inherited object is the 1st inherited object 使用来自数据库的javascript值根据1st更改2nd下拉值 - Changing 2nd dropdown values according to 1st using javascript values coming from database 在第一个数组元素中的第二个数组元素中查找字母 - Finding letters in 2nd array element in 1st array element 如果第二个数组匹配,则对第一个数组中的数字求和 - Summing numbers from 1st array if 2nd array match 有没有办法使用第一个数组作为 Javascript 中的参考在第二个数组中获取相应的索引值? - Is there a way to get corresponding index value in a 2nd array using the 1st array as a reference in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM