[英]Strategy to Loop Through 2D Array from Google Sheets To Create Entry for Each Date and ID
I have a Google Sheet that contains data I need to manipulate.我有一个 Google 表格,其中包含我需要处理的数据。 It is essentially a list of assignments by student.它本质上是学生的作业列表。 The original columns are:原始列是:
Name, ID, Assignment 1, Assignment 2, Assignment 3, Assignment 4, Date, Overall Grade
Using existing code, I am concatenating the Assignments into a single field and creating an array with these columns:使用现有代码,我将 Assignments 连接到一个字段中并创建一个包含这些列的数组:
ID, Name, Assignments, Date, Overall Grade
The resulting array looks like this:结果数组如下所示:
[ [ '1234',
'Santa Claus',
'US History Chapter 1.1, , , ',
Fri Nov 18 2022 00:00:00 GMT-0800 (Pacific Standard Time),
'B+' ],
[ '1234',
'Santa Claus',
'US History Chapter 2.1, US History Chapter 1.1, , ',
Thu Nov 17 2022 00:00:00 GMT-0800 (Pacific Standard Time),
'B' ],
[ '12222',
'Mary Poppins',
'US History Chapter 8, , , ',
Fri Nov 18 2022 00:00:00 GMT-0800 (Pacific Standard Time),
'A' ]]
On a separate sheet I have a list of dates.在单独的一张纸上,我有一个日期列表。 What I want is to have a line for every single student and date combination and the assignment data if it exists.我想要的是为每个学生和日期组合以及作业数据(如果存在)设置一行。 Eg If the dates are Nov 17, Nov 18, and Nov19
and there are 2 students with data in the list, there would be 6 total entries sorted by student then by date.例如,如果日期是Nov 17, Nov 18, and Nov19
中有 2 个学生有数据,则总共有 6 个条目按学生然后按日期排序。 For the Assignments
column, there would only be data if there was an assignment entered for that date and that student.对于“ Assignments
”列,只有在为该日期和该学生输入作业时才会有数据。 Otherwise it would be blank.否则它将是空白的。 For example:例如:
ID ID | Name姓名 | Assignments作业 | Date日期 | Grade年级 |
---|---|---|---|---|
1234 1234 | Santa Claus圣诞老人 | Chapter 2.1, US History Chapter 1.1第 2.1 章,美国历史第 1.1 章 | Nov17 11月17日 | B乙 |
1234 1234 | Santa Claus圣诞老人 | US History Chapter 1.1美国历史第 1.1 章 | Nov18 11月18日 | B+ B+ |
1234 1234 | Santa Claus圣诞老人 | Nov19 11月19日 | ||
12222 12222 | Mary Poppins欢乐满人间 | Nov17 11月17日 | ||
12222 12222 | Mary Poppins欢乐满人间 | US History Chapter 8美国历史第8章 | Nov 18 11 月 18 日 | A一种 |
12222 12222 | Mary Poppins欢乐满人间 | Nov19 11月19日 |
What I think is needed is something like this:我认为需要的是这样的:
I'm just not sure how to manipulate the array to do something like that.我只是不确定如何操纵数组来做类似的事情。 How would I approach this?我将如何处理这个?
Although, unfortunately, I cannot know your actual array, from your showing sample array and a sample table, how about the following sample script?虽然不幸的是,我无法从您显示的示例数组和示例表中知道您的实际数组,但以下示例脚本如何?
const array = [,,,]; // Please set your array.
const res = Object.entries(array.reduce((o, e) => (o[e[0]] = o[e[0]] ? [...o[e[0]], e] : [e], o), {}))
.sort(([a], [b]) => Number(a) > Number(b) ? 1 : -1)
.flatMap(([, v]) => v.sort((a, b) => a[3].getTime() > b[3].getTime() ? 1 : -1));
console.log(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.