如何在 Google Apps 脚本中的 2D 数组地图中从一行返回两行
我正在尝试获取从电子表格收到的人口普查数据并将其转换为二维数组以对其进行清理。不幸的是,并非我收到的所有工作表的格式都相同,因此我试图使脚本能够使用多种格式。当每人一行时,我可以使用它,但是当我为每个家属获取一行,为每个员工获取多行时,我的脚本不会返回所需的结果。
工作示例输入:
[
["LstNm", "1st Nm", "Email", "EE-SSN", "Mem-SSN","Relate"]
["Smith", "John", "[email protected]", 11, 11, "E"]
["Wang", "Jacob", "[email protected]", 11, 22, "C"]
["Da Silva", "Jingle", "[email protected]", 13, 13, "E"]
["Martin", "Heimer", "[email protected]", 12, 12, "E"]
["Muller", "Schmidt", "[email protected]", 12, 22, "S"]
]
问题输入。这是我想返回两行的地方,一行用于员工,一行用于家属。但在我的代码目前的状态下,它不起作用:
[
["LstNm", "1stNm", "Email", "EE-SSN", "Mem-SSN", "Relate", "DepLstNm", "Dep1stNm", "Dep-SSN", "Dep-Relate"]
["Smith", "John", "[email protected]", 11, 11, "E", "Wang", "Jacob", 21, "C"]
["Da Silva", "Jingle", "[email protected]", 13, 13, "E", , , , ]
["Martin", "Heimer", "[email protected]", 12, 12, "E", "Muller", "Schmidt", 22, "S"]
]
两种情况下的预期最终结果都是每人一行,其中 EE-SSN 将员工与家属联系起来:
[
["Email", "1stNm", "LstNm", "EE-SSN", "Mem-SSN", "Relate"]
["[email protected]", "John", "Smith", 11, 11, "E"]
["[email protected]", "Jacob", "Wang", 11, 22, "C"]
["[email protected]", "Jingle", "Da Silva", 13, 13, "E"]
["[email protected]", "Heimer", "Martin", 12, 12, "E"]
["[email protected]", "Schmidt", "Muller", 12, 22, "S"]
]
最后,这是代码我正在使用:
function getData(origData, index) {
var depCheck = index[6]+index[7]+index[8]+index[9]
if (depCheck < 0) {
var workData = origData.map(row => ([
row[index[2]], // EMAIL
row[index[1]], // FIRST NAME
row[index[0]], // LAST NAME
row[index[3]], // SUBSCIBER SSN
row[index[4]], // MEMBER SSN
row[index[5]], // RELATIONSHIP
]));
} else {
var arr = [[]];
var workData = origData.flatMap(row => {
arr.push([
row[index[2]], // EMAIL
row[index[1]], // FIRST NAME
row[index[0]], // LAST NAME
row[index[3]], // SUBSCIBER SSN
row[index[4]], // MEMBER SSN
row[index[5]], // RELATIONSHIP
]);
if (typeof row[index[6]] !== "undefined" && typeof row[index[8]] !== "undefined" ) {
arr.push([
row[index[2]], // EMAIL
row[index[7]], // FIRST NAME
row[index[6]], // LAST NAME
row[index[3]], // SUBSCIBER SSN
row[index[8]], // MEMBER SSN
row[index[9]], // RELATIONSHIP
]);
}
return arr;
});
}
return workData;
}
function main () {
const originalSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3');
const origData = originalSheet.getRange("A6:F11").getValues();
console.log(origData);
var index = [0,1,2,3,4,5,-1,-1,-1,-1];
var data = getData(origData, index);
console.log("Single Rows:\n" + data);
const origData2 = originalSheet.getRange("A13:J16").getValues();
console.log(origData2);
var index2 = [0,1,2,3,4,5,6,7,8,9];
var data2 = getData(origData2, index2);
console.log("Double Rows:\n" + data2);
var numRows = data.length;
var numCols = data[0].length;
var numRows2 = data2.length;
var numCols2 = data2[0].length;
originalSheet.getRange(18,1,numRows,numCols).setValues(data);
originalSheet.getRange(18,7,numRows2,numCols2).setValues(data2);
}
编辑进一步查看代码,我意识到通过在第二个地图中返回一个二维数组,该数组返回arr[tempEE, tempDep] 实际上正在成为3D 阵列。因此,我将地图切换为 flatMap (参见下面的答案),它给出了所需的 2D 数组。
I'm trying to take census data I receive from a spreadsheet and convert it to a 2D array to do my cleaning to it. Unfortunately, not all sheets I receive are formatted the same, and thus I am trying to make the script be able to work with multiple formats. I have it working when there is one line per person, but when I get one line per dependent and multiple per employee my script does not return the desired results.
Working Example inputs:
[
["LstNm", "1st Nm", "Email", "EE-SSN", "Mem-SSN","Relate"]
["Smith", "John", "[email protected]", 11, 11, "E"]
["Wang", "Jacob", "[email protected]", 11, 22, "C"]
["Da Silva", "Jingle", "[email protected]", 13, 13, "E"]
["Martin", "Heimer", "[email protected]", 12, 12, "E"]
["Muller", "Schmidt", "[email protected]", 12, 22, "S"]
]
Problem Inputs. This is where I want to return two rows, one for the employee, and one for the dependent. But in my code's present state, it is not working:
[
["LstNm", "1stNm", "Email", "EE-SSN", "Mem-SSN", "Relate", "DepLstNm", "Dep1stNm", "Dep-SSN", "Dep-Relate"]
["Smith", "John", "[email protected]", 11, 11, "E", "Wang", "Jacob", 21, "C"]
["Da Silva", "Jingle", "[email protected]", 13, 13, "E", , , , ]
["Martin", "Heimer", "[email protected]", 12, 12, "E", "Muller", "Schmidt", 22, "S"]
]
The expected end result in both cases would be One row per person with the EE-SSN linking the employee to the dependent:
[
["Email", "1stNm", "LstNm", "EE-SSN", "Mem-SSN", "Relate"]
["[email protected]", "John", "Smith", 11, 11, "E"]
["[email protected]", "Jacob", "Wang", 11, 22, "C"]
["[email protected]", "Jingle", "Da Silva", 13, 13, "E"]
["[email protected]", "Heimer", "Martin", 12, 12, "E"]
["[email protected]", "Schmidt", "Muller", 12, 22, "S"]
]
Finally, here is the code I'm using:
function getData(origData, index) {
var depCheck = index[6]+index[7]+index[8]+index[9]
if (depCheck < 0) {
var workData = origData.map(row => ([
row[index[2]], // EMAIL
row[index[1]], // FIRST NAME
row[index[0]], // LAST NAME
row[index[3]], // SUBSCIBER SSN
row[index[4]], // MEMBER SSN
row[index[5]], // RELATIONSHIP
]));
} else {
var arr = [[]];
var workData = origData.flatMap(row => {
arr.push([
row[index[2]], // EMAIL
row[index[1]], // FIRST NAME
row[index[0]], // LAST NAME
row[index[3]], // SUBSCIBER SSN
row[index[4]], // MEMBER SSN
row[index[5]], // RELATIONSHIP
]);
if (typeof row[index[6]] !== "undefined" && typeof row[index[8]] !== "undefined" ) {
arr.push([
row[index[2]], // EMAIL
row[index[7]], // FIRST NAME
row[index[6]], // LAST NAME
row[index[3]], // SUBSCIBER SSN
row[index[8]], // MEMBER SSN
row[index[9]], // RELATIONSHIP
]);
}
return arr;
});
}
return workData;
}
function main () {
const originalSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3');
const origData = originalSheet.getRange("A6:F11").getValues();
console.log(origData);
var index = [0,1,2,3,4,5,-1,-1,-1,-1];
var data = getData(origData, index);
console.log("Single Rows:\n" + data);
const origData2 = originalSheet.getRange("A13:J16").getValues();
console.log(origData2);
var index2 = [0,1,2,3,4,5,6,7,8,9];
var data2 = getData(origData2, index2);
console.log("Double Rows:\n" + data2);
var numRows = data.length;
var numCols = data[0].length;
var numRows2 = data2.length;
var numCols2 = data2[0].length;
originalSheet.getRange(18,1,numRows,numCols).setValues(data);
originalSheet.getRange(18,7,numRows2,numCols2).setValues(data2);
}
EDIT Reviewing the code further, I realized that by returning a 2D array in the second map which returns arr[tempEE, tempDep]
is actually becoming a 3D array. As such, I switched the map to flatMap (see answer below), which gives the desired 2D array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
说明
以下是如何使用数组函数将员工/受抚养人行分成 2 个单独的行的修订示例。此示例假设每个员工只有一名受抚养人。
脚本
Console.log
参考
Description
Here is a revised example of how to use array functions to break the employee/dependant row into 2 seperate rows . This example assumes there is only one dependant per employee.
Script
Console.log
Reference
实际上,将第二个地图更改为 flatMap 确实可以解决问题,这是我使用的最终代码:
参考: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap
Actually, changing the second map to flatMap does fix the problem, here is the final code I used:
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap