簡體   English   中英

使用 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創建一個 object。 並且,使用parametrosRisco ,您的預期值是從 object 中檢索的。

參考:

逐行構建二維數組

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

我的工作表:

部分 1個
姓名1 價值11
名字2 值2
名字3 值3
姓名4 值4 名字5 值5
名字6 價值6
名字7 值7
名字8 值8
名字9 價值19
部分 2個
姓名1 價值21
名字2 值2
名字3 值3
姓名4 值4 名字5 值5
名字6 價值6
名字7 值7
名字8 值8
名字9 價值29
部分 3個
姓名1 值31
名字2 值2
名字3 值3
姓名4 值4 名字5 值5
名字6 價值6
名字7 值7
名字8 值8
名字9 值39

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM