【问题标题】:Google Apps Scripts - For Loop Issue works on false statement instead of trueGoogle Apps 脚本 - For Loop 问题适用于虚假陈述而不是真实
【发布时间】:2021-07-24 17:29:05
【问题描述】:
/** -- For Loop to start pairing VIN numbers to parts list -- **/
  console.log('CompleteVIN: ');
  console.log(completeVIN);
  console.log(completeVIN[1].length);

  console.log('CompleteParts: ');
  console.log(completeParts);
  console.log(completeParts[1].length);

  var vinPartsCombine = [];

  /** For loop */
  for (j = 0; j < completeVIN[1].length; j++) {
    for (i = 0; i < completeParts[0].length; i++){    
      if (completeVIN[1][j] === completeParts[1][i]) {
        vinPartsCombine.push(completeVIN[0][j],completeParts[0][i]);
      };
    };
  };
  console.log(completeVIN[1][j] === completeParts[1][i]);
  console.log(vinPartsCombine);

结果:

CompleteVIN: 
[ [ [ 'KG257653' ],
    [ 'KG250444' ],
    [ 'K1239114' ],
    [ 'FG160157' ],
    [ 'LKA41783' ],
    [ 'LKB72564' ] ],
  [ [ 7653 ],
    [ 444 ],
    [ 9114 ],
    [ 'Estimate_to_Repair' ],
    [ 'transit (1)' ],
    [ 'transit' ] ] ]
6
CompleteParts: 
[ [ [ 'Repl LT Side bracket' ],
    [ 'Repl LT Filler panel US built' ],
    [ 'Repl LT H\'lamp bracket' ],
    [ 'Repl LT Fender' ],
    [ 'Repl LT Nameplate "SILVERADO"' ],
    [ 'Repl Bumper assy single' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Applique upper w/o special trim' ],
    [ 'Repl Corrosion protection primer' ],
    [ 'Repl Cover Car' ],
    [ 'Replace LT Fender (Aftermarket)' ],
    [ 'Replace LT Nameplate "SILVERADO"' ],
    [ 'Repl LT Lower molding' ],
    [ 'Repl LT End cap w/o blind spot' ],
    [ 'Repl LT Side extn w/o extended frame' ],
    [ 'Repl LT Bumper outer support' ],
    [ 'Repl LT Bumper inner support' ],
    [ 'Repl LT Bumper' ],
    [ 'Repl Aerial assembly' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Corrosion protection primer' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Corrosion protection primer' ] ],
  [ [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 444 ],
    [ 9114 ],
    [ 9114 ],
    [ 9114 ],
    [ 'Estimate_to_Repair' ],
    [ 'Estimate_to_Repair' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit' ],
    [ 'transit' ],
    [ 'transit' ] ] ]
25
true
[]

我的 for 循环不会按我想要的方式工作,但是当我将条件更改为 != 时它会运行,但不是我想要的输出。

  • 完整的 VIN 数组由 VIN 号和源名称组成
  • CompleteParts 数组由 Parts 和 Source Names 组成

for 循环是在源匹配时按源搜索每个数组,然后将其推送到 vinPartsCombine 数组,以便将其与 vin 编号和部件描述配对。在源不匹配之前,它将切换到具有新源名称的下一个 vin 编号元素。

completeVIN = [[[ 'KG257653' ],
    [ 'KG250444' ],
    [ 'K1239114' ],
    [ 'FG160157' ],
    [ 'LKA41783' ],
    [ 'LKB72564' ]],
    [[ 7653 ],
    [ 444 ],
    [ 9114 ],
    [ 'Estimate_to_Repair' ],
    [ 'transit (1)' ],
    [ 'transit' ]]];
    
    
CompleteParts = [ [ [ 'Repl LT Side bracket' ],
    [ 'Repl LT Filler panel US built' ],
    [ 'Repl LT H\'lamp bracket' ],
    [ 'Repl LT Fender' ],
    [ 'Repl LT Nameplate "SILVERADO"' ],
    [ 'Repl Bumper assy single' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Applique upper w/o special trim' ],
    [ 'Repl Corrosion protection primer' ],
    [ 'Repl Cover Car' ],
    [ 'Replace LT Fender (Aftermarket)' ],
    [ 'Replace LT Nameplate "SILVERADO"' ],
    [ 'Repl LT Lower molding' ],
    [ 'Repl LT End cap w/o blind spot' ],
    [ 'Repl LT Side extn w/o extended frame' ],
    [ 'Repl LT Bumper outer support' ],
    [ 'Repl LT Bumper inner support' ],
    [ 'Repl LT Bumper' ],
    [ 'Repl Aerial assembly' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Corrosion protection primer' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Cover Car' ],
    [ 'Repl Corrosion protection primer' ] ],
  [ [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 7653 ],
    [ 444 ],
    [ 9114 ],
    [ 9114 ],
    [ 9114 ],
    [ 'Estimate_to_Repair' ],
    [ 'Estimate_to_Repair' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit (1)' ],
    [ 'transit' ],
    [ 'transit' ],
    [ 'transit' ] ] ];

var vinPartsCombine = [];

  /** For loop */
  for (j = 0; j < completeVIN[1].length; j++) {
    for (i = 0; i < CompleteParts[1].length; i++){    
      if (completeVIN[1][j] == CompleteParts[1][i]) {
        vinPartsCombine.push(completeVIN[0][j],CompleteParts[0][i]);
      };
    };
  };
  console.log(completeVIN[1][j] === CompleteParts[1][i]);
  console.log(vinPartsCombine);

【问题讨论】:

  • 仅仅是因为你写的是completeParts[0].length而不是completeParts[1].length(“1”)吗?
  • 我在第一个循环中这样做是因为我希望我的 j 值在我的 completeVIN 数组中循环 6 次。因为那个只有 6 个值,或者 6 个源要查看。虽然我的部件数组有 25 个值并且源在该数组中重复,所以应该有多个匹配项。每个源值。 1 是源列,但这并不重要,因为 [0] 或 [1] 的长度相同。如果我只使用 completeParts.length 则长度为 2。
  • 好吧有道理。我稍微清理了日志以使其更具可读性。我还建议您提供一个(仍然相关的)示例,其中的项目较少,只是为了更容易阅读/推理
  • 好的,我想我在 sn-p 中重新创建了相同的问题。感谢您的帮助,谢谢。唯一的问题是谷歌脚本使用 (.push) 我会在 JavaScript 中使用 append()

标签: javascript arrays for-loop google-apps-script google-sheets


【解决方案1】:

First Function 在电子表格上构建了几个表格,我用这些表格来弄清楚发生了什么。第二个函数使用电子表格中的这些表来汇总数据,我认为通过旋转数据以您想要的方式。

function buildtablescript() {
  const completeVin = [[['KG257653'], ['KG250444'], ['K1239114'], ['FG160157'], ['LKA41783'], ['LKB72564']], [[7653], [444], [9114], ['Estimate_to_Repair'], ['transit (1)'], ['transit']]];
  const completeParts = [[['Repl LT Side bracket'], ['Repl LT Filler panel US built'], ['Repl LT H\'lamp bracket'], ['Repl LT Fender'], ['Repl LT Nameplate "SILVERADO"'], ['Repl Bumper assy single'], ['Repl Cover Car'], ['Repl Cover Car'], ['Repl Applique upper w/o special trim'], ['Repl Corrosion protection primer'], ['Repl Cover Car'], ['Replace LT Fender (Aftermarket)'], ['Replace LT Nameplate "SILVERADO"'], ['Repl LT Lower molding'], ['Repl LT End cap w/o blind spot'], ['Repl LT Side extn w/o extended frame'], ['Repl LT Bumper outer support'], ['Repl LT Bumper inner support'], ['Repl LT Bumper'], ['Repl Aerial assembly'], ['Repl Cover Car'], ['Repl Corrosion protection primer'], ['Repl Cover Car'], ['Repl Cover Car'], ['Repl Corrosion protection primer']], [[7653], [7653], [7653], [7653], [7653], [7653], [7653], [444], [9114], [9114], [9114], ['Estimate_to_Repair'], ['Estimate_to_Repair'], ['transit (1)'], ['transit (1)'], ['transit (1)'], ['transit (1)'], ['transit (1)'], ['transit (1)'], ['transit (1)'], ['transit (1)'], ['transit (1)'], ['transit'], ['transit'], ['transit']]];
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet4');
  sh.getDataRange().setFontWeight('normal').setBackground('#ffffff');
  sh.clearContents();
  sh.getRange(1, 1).setValue('CompleteVINs:').setFontWeight('bold').setBackground('lightyellow');
  let nr = sh.getLastRow() + 1;
  sh.getRange(nr, 1, completeVin[0].length, completeVin[0][0].length).setValues(completeVin[0]).setHorizontalAlignment('left');
  sh.getRange(nr, 2, completeVin[1].length, completeVin[1][0].length).setValues(completeVin[1]).setHorizontalAlignment('left');
  SpreadsheetApp.flush();
  nr = sh.getLastRow() + 1;
  sh.getRange(nr, 1).setValue('CompleteParts:').setFontWeight('bold').setBackground('lightyellow');
  SpreadsheetApp.flush();
  nr = sh.getLastRow() + 1;
  sh.getRange(nr, 1, completeParts[0].length, completeParts[0][0].length).setValues(completeParts[0]).setHorizontalAlignment('left');
  sh.getRange(nr, 2, completeParts[1].length, completeParts[1][0].length).setValues(completeParts[1]).setHorizontalAlignment('left');
}

上述函数产生的表格:

CompleteVINs:
KG257653 7653
KG250444 444
K1239114 9114
FG160157 Estimate_to_Repair
LKA41783 transit (1)
LKB72564 transit
CompleteParts:
Repl LT Side bracket 7653
Repl LT Filler panel US built 7653
Repl LT H'lamp bracket 7653
Repl LT Fender 7653
Repl LT Nameplate "SILVERADO" 7653
Repl Bumper assy single 7653
Repl Cover Car 7653
Repl Cover Car 444
Repl Applique upper w/o special trim 9114
Repl Corrosion protection primer 9114
Repl Cover Car 9114
Replace LT Fender (Aftermarket) Estimate_to_Repair
Replace LT Nameplate "SILVERADO" Estimate_to_Repair
Repl LT Lower molding transit (1)
Repl LT End cap w/o blind spot transit (1)
Repl LT Side extn w/o extended frame transit (1)
Repl LT Bumper outer support transit (1)
Repl LT Bumper inner support transit (1)
Repl LT Bumper transit (1)
Repl Aerial assembly transit (1)
Repl Cover Car transit (1)
Repl Corrosion protection primer transit (1)
Repl Cover Car transit
Repl Cover Car transit
Repl Corrosion protection primer transit

现在是汇总数据的函数:

function pivotscript() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet4');
  const cVrow = sh.createTextFinder('CompleteVINs:').findAll()[0].getRow();
  const cProw = sh.createTextFinder('CompleteParts:').findAll()[0].getRow();
  const cVvs = sh.getRange(cVrow + 1, 1, cProw - cVrow - 1, sh.getLastColumn()).getValues();
  const cPvs = sh.getRange(cProw + 1, 1, sh.getLastRow() - cProw - 1, sh.getLastColumn()).getValues();
  const pObj = { pA: [], vA: [] };
  cVvs.forEach((r, i) => {
    if (!pObj.hasOwnProperty(r[1])) {
      pObj[r[1]] = {vin:r[0],partA:[]};
      pObj.pA.push(r[1]);
    } else {
      SpreadsheetApp.getUi().alert('Vin Table has duplicates');
      return;
    }
  });//build the table
  cPvs.forEach((r, i) => {if (pObj.hasOwnProperty(r[1])) {pObj[r[1]].partA.push(r[0]);}});
  let html = '<style>th,td{border:1px solid black;padding:5px;}</style><table><tr><th>Property</th><th>Vin</th><th>Parts</th></tr>';
  pObj.pA.forEach((p,i)=>{
    html+=Utilities.formatString('<tr><td>%s</td><td>%s</td><td>%s</td></tr>',p,pObj[p].vin,pObj[p].partA.join('<br />'));
  });
  html+='</table>'
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html).setWidth(800).setHeight(600),'Summary');
}

汇总表对话框:

【讨论】:

    【解决方案2】:

    我想通了。我最初通过在元素中添加元素使其变得更加复杂......例如:[0][0][0]。无论如何,这就是我所做的。

    completeVIN = [[[ 'KG257653' ],
        [ 'KG250444' ],
        [ 'K1239114' ],
        [ 'FG160157' ],
        [ 'LKA41783' ],
        [ 'LKB72564' ]],
        [[ 7653 ],
        [ 444 ],
        [ 9114 ],
        [ 'Estimate_to_Repair' ],
        [ 'transit (1)' ],
        [ 'transit' ]]];
        
        
    completeParts = [ [ [ 'Repl LT Side bracket' ],
        [ 'Repl LT Filler panel US built' ],
        [ 'Repl LT H\'lamp bracket' ],
        [ 'Repl LT Fender' ],
        [ 'Repl LT Nameplate "SILVERADO"' ],
        [ 'Repl Bumper assy single' ],
        [ 'Repl Cover Car' ],
        [ 'Repl Cover Car' ],
        [ 'Repl Applique upper w/o special trim' ],
        [ 'Repl Corrosion protection primer' ],
        [ 'Repl Cover Car' ],
        [ 'Replace LT Fender (Aftermarket)' ],
        [ 'Replace LT Nameplate "SILVERADO"' ],
        [ 'Repl LT Lower molding' ],
        [ 'Repl LT End cap w/o blind spot' ],
        [ 'Repl LT Side extn w/o extended frame' ],
        [ 'Repl LT Bumper outer support' ],
        [ 'Repl LT Bumper inner support' ],
        [ 'Repl LT Bumper' ],
        [ 'Repl Aerial assembly' ],
        [ 'Repl Cover Car' ],
        [ 'Repl Corrosion protection primer' ],
        [ 'Repl Cover Car' ],
        [ 'Repl Cover Car' ],
        [ 'Repl Corrosion protection primer' ] ],
      [ [ 7653 ],
        [ 7653 ],
        [ 7653 ],
        [ 7653 ],
        [ 7653 ],
        [ 7653 ],
        [ 7653 ],
        [ 444 ],
        [ 9114 ],
        [ 9114 ],
        [ 9114 ],
        [ 'Estimate_to_Repair' ],
        [ 'Estimate_to_Repair' ],
        [ 'transit (1)' ],
        [ 'transit (1)' ],
        [ 'transit (1)' ],
        [ 'transit (1)' ],
        [ 'transit (1)' ],
        [ 'transit (1)' ],
        [ 'transit (1)' ],
        [ 'transit (1)' ],
        [ 'transit (1)' ],
        [ 'transit' ],
        [ 'transit' ],
        [ 'transit' ] ] ];
    
    var vinPartsCombine = [];
    
      /** For loop */
      for (j = 0; j < completeVIN[1].length; j++) {
        for (i = 0; i < completeParts[1].length; i++){  
          /** 
          console.log(completeVIN[1][j]);
          console.log(completeParts[1][i]);
          console.log(typeof(completeVIN[1][j]));
          console.log(typeof(completeParts[1][i]));
          */ 
    
          console.log(completeVIN[1][j][0] == completeParts[1][i][0]);
    
          if (completeVIN[1][j][0] == completeParts[1][i][0]) {
            vinPartsCombine.push([completeVIN[0][j][0],completeParts[0][i][0]]);
          };
        };
      };
      /** 
      console.log(completeVIN[1][j] === completeParts[1][i]);
      console.log(typeof(completeVIN[1][0][0]));
      */
      console.log(vinPartsCombine);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-31
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多