使用 Google Apps 脚本循环遍历数据集时如何构建二维数组?
How to build 2D array when looping through sets of data using Google Apps Script?
因此,数据如下所示:
下面的代码像这样构建一个数组:
[1,"Forro","Teste Molde",2,"36 + 38 + 40 + 42",4,8,"Não Espelhado","Tecido/Pé","Obs",2,"Tag Código Produto","Molde 2",5,"36 + 40",2,10,"Sim","Tecido/Pé2","Obs 2"]
但是需要这样,从Risco
开始,到Obs
结束:
[
[1,"Forno","Teste Molde",2,"36 + 38 + 40 + 42",4,8,"Não Espelhado","Tecido/Pé","Obs"],
[2,"Tag Código Produto","Molde 2",5,"36 + 40",2,10,"Sim","Tecido/Pé2","Obs 2"]
]
这是我正在研究的代码:
function salvarCorte(status) {
if (status != '') {
const dadosCorte = sheetCorte.getRange(1, 1, sheetCorte.getLastRow(), sheetCorte.getLastColumn()).getValues();
let outerArray= [];
var innerArray = [];
const parametrosRisco = ["Risco", "Matéria Prima", "Molde", "Tamanho", "Grade", "Consumo Unit.", "Espelhado", "Tecido/Pé", "Obs", "Qtd/Peças"];
let startedArray = false
for (let r = 0; r < dadosCorte.length; r++) {
if (dadosCorte[r][0] == 'Risco') {
startedArray = true
}
if (startedArray == true) {
if (parametrosRisco.indexOf(dadosCorte[r][0]) > -1) {
innerArray .push(dadosCorte[r][1]);
}
if (parametrosRisco.indexOf(dadosCorte[r][2]) > -1) {
innerArray .push(dadosCorte[r][3]);
}
if (dadosCorte[r][0] == 'Obs') {
startedArray = false;
}
}
}
outerArray.concat(innerArray )
}
}
感谢您的帮助!
当我看到您的脚本时,来自 outerArray.concat(innerArray )
,我认为您当前的价值无法复制。所以,我担心您的显示脚本可能与复制您的显示第一个值的脚本不同。
那么,在您的情况下,下面的示例脚本怎么样?
示例脚本:
var sheetCorte = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet"); // Please set the sheet name.
const dadosCorte = sheetCorte.getRange(1, 1, sheetCorte.getLastRow(), sheetCorte.getLastColumn()).getValues();
const parametrosRisco = ["Risco", "Matéria Prima", "Molde", "Tamanho", "Grade", "Qtd/Peças", "Consumo Unit.", "Espelhado", "Tecido/Pé", "Obs"];
const obj = dadosCorte.reduce((o, [a, b, c, d]) => {
if (a && parametrosRisco.includes(a)) o[a] = o[a] ? [...o[a], b] : [b];
if (c && parametrosRisco.includes(c)) o[c] = o[c] ? [...o[c], d] : [d];
return o;
}, {});
const v = parametrosRisco.map(e => obj[e]);
const res = v[0].map((_, c) => v.map(r => r[c]));
console.log(res)
- 在此示例中,首先使用
parametrosRisco
创建一个对象。并且,使用 parametrosRisco
,从对象中检索您的预期值。
参考文献:
逐行构建二维数组
function myfunk() {
const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet0"); // Please set the sheet name.
const vs = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn()).getValues();
const headers = ["Section", "Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8", "Name9"];
const obj = vs.reduce((o, [a, b, c, d], i) => {
if (a && headers.includes(a)) {
o.row.push(b);
if(o.row.length == headers.length) {
o.r.push(o.row);
o.row = [];
}
}
if (c && headers.includes(c)) {
o.row.push(d);
}
return o;
}, { row: [],r: [] });
console.log(obj.r);
}
Execution log
11:30:09 PM Notice Execution started
11:30:10 PM Info [ [ 1,
'Value11',
'Value2',
'Value3',
'Value4',
'Value5',
'Value6',
'Value7',
'Value8',
'Value19' ],
[ 2,
'Value21',
'Value2',
'Value3',
'Value4',
'Value5',
'Value6',
'Value7',
'Value8',
'Value29' ],
[ 3,
'Value31',
'Value2',
'Value3',
'Value4',
'Value5',
'Value6',
'Value7',
'Value8',
'Value39' ] ]
11:30:11 PM Notice Execution completed
我的Sheet:
Section
1
Name1
Value11
Name2
Value2
Name3
Value3
Name4
Value4
Name5
Value5
Name6
Value6
Name7
Value7
Name8
Value8
Name9
Value19
Section
2
Name1
Value21
Name2
Value2
Name3
Value3
Name4
Value4
Name5
Value5
Name6
Value6
Name7
Value7
Name8
Value8
Name9
Value29
Section
3
Name1
Value31
Name2
Value2
Name3
Value3
Name4
Value4
Name5
Value5
Name6
Value6
Name7
Value7
Name8
Value8
Name9
Value39
因此,数据如下所示:
下面的代码像这样构建一个数组:
[1,"Forro","Teste Molde",2,"36 + 38 + 40 + 42",4,8,"Não Espelhado","Tecido/Pé","Obs",2,"Tag Código Produto","Molde 2",5,"36 + 40",2,10,"Sim","Tecido/Pé2","Obs 2"]
但是需要这样,从Risco
开始,到Obs
结束:
[
[1,"Forno","Teste Molde",2,"36 + 38 + 40 + 42",4,8,"Não Espelhado","Tecido/Pé","Obs"],
[2,"Tag Código Produto","Molde 2",5,"36 + 40",2,10,"Sim","Tecido/Pé2","Obs 2"]
]
这是我正在研究的代码:
function salvarCorte(status) {
if (status != '') {
const dadosCorte = sheetCorte.getRange(1, 1, sheetCorte.getLastRow(), sheetCorte.getLastColumn()).getValues();
let outerArray= [];
var innerArray = [];
const parametrosRisco = ["Risco", "Matéria Prima", "Molde", "Tamanho", "Grade", "Consumo Unit.", "Espelhado", "Tecido/Pé", "Obs", "Qtd/Peças"];
let startedArray = false
for (let r = 0; r < dadosCorte.length; r++) {
if (dadosCorte[r][0] == 'Risco') {
startedArray = true
}
if (startedArray == true) {
if (parametrosRisco.indexOf(dadosCorte[r][0]) > -1) {
innerArray .push(dadosCorte[r][1]);
}
if (parametrosRisco.indexOf(dadosCorte[r][2]) > -1) {
innerArray .push(dadosCorte[r][3]);
}
if (dadosCorte[r][0] == 'Obs') {
startedArray = false;
}
}
}
outerArray.concat(innerArray )
}
}
感谢您的帮助!
当我看到您的脚本时,来自 outerArray.concat(innerArray )
,我认为您当前的价值无法复制。所以,我担心您的显示脚本可能与复制您的显示第一个值的脚本不同。
那么,在您的情况下,下面的示例脚本怎么样?
示例脚本:
var sheetCorte = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet"); // Please set the sheet name.
const dadosCorte = sheetCorte.getRange(1, 1, sheetCorte.getLastRow(), sheetCorte.getLastColumn()).getValues();
const parametrosRisco = ["Risco", "Matéria Prima", "Molde", "Tamanho", "Grade", "Qtd/Peças", "Consumo Unit.", "Espelhado", "Tecido/Pé", "Obs"];
const obj = dadosCorte.reduce((o, [a, b, c, d]) => {
if (a && parametrosRisco.includes(a)) o[a] = o[a] ? [...o[a], b] : [b];
if (c && parametrosRisco.includes(c)) o[c] = o[c] ? [...o[c], d] : [d];
return o;
}, {});
const v = parametrosRisco.map(e => obj[e]);
const res = v[0].map((_, c) => v.map(r => r[c]));
console.log(res)
- 在此示例中,首先使用
parametrosRisco
创建一个对象。并且,使用parametrosRisco
,从对象中检索您的预期值。
参考文献:
逐行构建二维数组
function myfunk() {
const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet0"); // Please set the sheet name.
const vs = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn()).getValues();
const headers = ["Section", "Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8", "Name9"];
const obj = vs.reduce((o, [a, b, c, d], i) => {
if (a && headers.includes(a)) {
o.row.push(b);
if(o.row.length == headers.length) {
o.r.push(o.row);
o.row = [];
}
}
if (c && headers.includes(c)) {
o.row.push(d);
}
return o;
}, { row: [],r: [] });
console.log(obj.r);
}
Execution log
11:30:09 PM Notice Execution started
11:30:10 PM Info [ [ 1,
'Value11',
'Value2',
'Value3',
'Value4',
'Value5',
'Value6',
'Value7',
'Value8',
'Value19' ],
[ 2,
'Value21',
'Value2',
'Value3',
'Value4',
'Value5',
'Value6',
'Value7',
'Value8',
'Value29' ],
[ 3,
'Value31',
'Value2',
'Value3',
'Value4',
'Value5',
'Value6',
'Value7',
'Value8',
'Value39' ] ]
11:30:11 PM Notice Execution completed
我的Sheet:
Section | 1 | ||
---|---|---|---|
Name1 | Value11 | ||
Name2 | Value2 | ||
Name3 | Value3 | ||
Name4 | Value4 | Name5 | Value5 |
Name6 | Value6 | ||
Name7 | Value7 | ||
Name8 | Value8 | ||
Name9 | Value19 | ||
Section | 2 | ||
Name1 | Value21 | ||
Name2 | Value2 | ||
Name3 | Value3 | ||
Name4 | Value4 | Name5 | Value5 |
Name6 | Value6 | ||
Name7 | Value7 | ||
Name8 | Value8 | ||
Name9 | Value29 | ||
Section | 3 | ||
Name1 | Value31 | ||
Name2 | Value2 | ||
Name3 | Value3 | ||
Name4 | Value4 | Name5 | Value5 |
Name6 | Value6 | ||
Name7 | Value7 | ||
Name8 | Value8 | ||
Name9 | Value39 |