简体   繁体   English

循环遍历 Google 表格中的二维数组以创建每个日期和 ID 条目的策略

[英]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:我认为需要的是这样的:

  1. Get unique list of all student IDs from the array从数组中获取所有学生 ID 的唯一列表
  2. Get list of dates from the sheet从工作表中获取日期列表
  3. Use a nested loop to go through each date and each student to create the lines.使用嵌套循环到 go 通过每个日期和每个学生来创建行。 If there is a match for the assignment data, add it, if not, leave that blank.如果分配数据匹配,则添加它,如果不匹配,则将其留空。

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?虽然不幸的是,我无法从您显示的示例数组和示例表中知道您的实际数组,但以下示例脚本如何?

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)
  • In this sample script, first, an object is created by checking the column "A".在此示例脚本中,首先,通过检查“A”列创建一个 object。 And, sort the array by the column "A".并且,按“A”列对数组进行排序。 And, for each element, sort the column "D".并且,对于每个元素,对“D”列进行排序。 And, all values are merged as a 2-dimensional array.并且,所有值都合并为一个二维数组。

Note:笔记:

  • If this sample script doesn't return your exected values, can you provide the detailed sample input values and the sample output values?如果此示例脚本未返回您的预期值,您能否提供详细的示例输入值和示例 output 值? By this, I would like to confirm it.借此,我想确认一下。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM