Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过匹配类似列javascript es6上的元素来连接两个多维数组_Javascript_Google Apps Script_Es6 Promise - Fatal编程技术网

通过匹配类似列javascript es6上的元素来连接两个多维数组

通过匹配类似列javascript es6上的元素来连接两个多维数组,javascript,google-apps-script,es6-promise,Javascript,Google Apps Script,Es6 Promise,我在运行chrome V8的Google sheets中工作 我有两个数组,由两张不同的表上的范围数据组成 在现实生活中,我确实预先知道arr1或arr2的宽度或长度。我所知道的是它们不是相同的维度 两个数组都有一个Id列。arr1将始终将Id列作为第一列。我事先不知道arr2中Id列的列位置 arr1如果打印到一张纸上 Id |Label |SAH1 |SAH2 |SAH3 --------------------------------------- aa1 |Bob |

我在运行chrome V8的Google sheets中工作

我有两个数组,由两张不同的表上的范围数据组成

在现实生活中,我确实预先知道arr1或arr2的宽度或长度。我所知道的是它们不是相同的维度

两个数组都有一个
Id
列。arr1将始终将
Id
列作为第一列。我事先不知道arr2中
Id
列的列位置

arr1如果打印到一张纸上

Id  |Label  |SAH1   |SAH2    |SAH3
---------------------------------------
aa1 |Bob    |stuff1 |stuff10 |stuff19
aa2 |Eggbert|stuff2 |stuff11 |stuff20
aa3 |Juan   |stuff3 |stuff12 |stuff21
aa4 |Alice  |stuff4 |stuff13 |stuff22
aa5 |Fraya  |stuff5 |stuff14 |stuff23
aa6 |Ted    |stuff6 |stuff15 |stuff24
aa7 |Velma  |stuff7 |stuff16 |stuff25
aa8 |Carol  |stuff8 |stuff17 |stuff26
aa9 |LeRoy  |stuff9 |stuff18 |stuff27
Name             |SXH1      |Id     |SXH2
--------------------------------------------
Maya Townsend    |thing1    |mm1    |thing20
David Collste    |thing2    |mm2    |thing21
Bob              |thing3    |aa1    |thing22
Jennifer Menke   |thing4    |mm4    |thing23
Aissata Camara   |thing5    |mm5    |thing24
Marta Arranz     |thing6    |mm6    |thing25
Michael  Lennon  |thing7    |mm7    |thing26
Jonny Norton     |thing8    |mm8    |thing27
Carol            |thing9    |aa8    |thing28
Ted              |thing10   |aa6    |thing29
Katie Boone      |thing11   |mm11   |thing30
Adeline Sibanda  |thing12   |mm12   |thing31
Brian Blankinship|thing13   |mm13   |thing32
Christine Lai    |thing14   |mm14   |thing33
Jennifer Pierce  |thing15   |mm15   |thing34
Federico Bellone |thing16   |mm16   |thing35
Jeffrey Su       |thing17   |mm17   |thing36
Alice            |thing18   |aa4    |thing37
Sarah Ditton     |thing19   |mm19   |thing38
Id  |Label  |SAH1   |SAH2    |SAH3    |Name |SXH1   |SXH2  
------------------------------------------------------------
aa1 |Bob    |stuff1 |stuff10 |stuff19 |Bob  |thing3 |thing22 
aa2 |Eggbert|stuff2 |stuff11 |stuff20 |""   |""     |""
aa3 |Juan   |stuff3 |stuff12 |stuff21 |""   |""     |""
aa4 |Alice  |stuff4 |stuff13 |stuff22 |Alice|thing18|thing37
aa5 |Fraya  |stuff5 |stuff14 |stuff23 |""   |""     |""
aa6 |Ted    |stuff6 |stuff15 |stuff24 |Ted  |thing10|thing29
aa7 |Velma  |stuff7 |stuff16 |stuff25 |""   |""     |""
aa8 |Carol  |stuff8 |stuff17 |stuff26 |Carol|thing9 |thing28
aa9 |LeRoy  |stuff9 |stuff18 |stuff27 |""   |""     |""
arr2如果打印到一张纸上

Id  |Label  |SAH1   |SAH2    |SAH3
---------------------------------------
aa1 |Bob    |stuff1 |stuff10 |stuff19
aa2 |Eggbert|stuff2 |stuff11 |stuff20
aa3 |Juan   |stuff3 |stuff12 |stuff21
aa4 |Alice  |stuff4 |stuff13 |stuff22
aa5 |Fraya  |stuff5 |stuff14 |stuff23
aa6 |Ted    |stuff6 |stuff15 |stuff24
aa7 |Velma  |stuff7 |stuff16 |stuff25
aa8 |Carol  |stuff8 |stuff17 |stuff26
aa9 |LeRoy  |stuff9 |stuff18 |stuff27
Name             |SXH1      |Id     |SXH2
--------------------------------------------
Maya Townsend    |thing1    |mm1    |thing20
David Collste    |thing2    |mm2    |thing21
Bob              |thing3    |aa1    |thing22
Jennifer Menke   |thing4    |mm4    |thing23
Aissata Camara   |thing5    |mm5    |thing24
Marta Arranz     |thing6    |mm6    |thing25
Michael  Lennon  |thing7    |mm7    |thing26
Jonny Norton     |thing8    |mm8    |thing27
Carol            |thing9    |aa8    |thing28
Ted              |thing10   |aa6    |thing29
Katie Boone      |thing11   |mm11   |thing30
Adeline Sibanda  |thing12   |mm12   |thing31
Brian Blankinship|thing13   |mm13   |thing32
Christine Lai    |thing14   |mm14   |thing33
Jennifer Pierce  |thing15   |mm15   |thing34
Federico Bellone |thing16   |mm16   |thing35
Jeffrey Su       |thing17   |mm17   |thing36
Alice            |thing18   |aa4    |thing37
Sarah Ditton     |thing19   |mm19   |thing38
Id  |Label  |SAH1   |SAH2    |SAH3    |Name |SXH1   |SXH2  
------------------------------------------------------------
aa1 |Bob    |stuff1 |stuff10 |stuff19 |Bob  |thing3 |thing22 
aa2 |Eggbert|stuff2 |stuff11 |stuff20 |""   |""     |""
aa3 |Juan   |stuff3 |stuff12 |stuff21 |""   |""     |""
aa4 |Alice  |stuff4 |stuff13 |stuff22 |Alice|thing18|thing37
aa5 |Fraya  |stuff5 |stuff14 |stuff23 |""   |""     |""
aa6 |Ted    |stuff6 |stuff15 |stuff24 |Ted  |thing10|thing29
aa7 |Velma  |stuff7 |stuff16 |stuff25 |""   |""     |""
aa8 |Carol  |stuff8 |stuff17 |stuff26 |Carol|thing9 |thing28
aa9 |LeRoy  |stuff9 |stuff18 |stuff27 |""   |""     |""
如果打印到图纸上,结果arr需要如下所示

Id  |Label  |SAH1   |SAH2    |SAH3
---------------------------------------
aa1 |Bob    |stuff1 |stuff10 |stuff19
aa2 |Eggbert|stuff2 |stuff11 |stuff20
aa3 |Juan   |stuff3 |stuff12 |stuff21
aa4 |Alice  |stuff4 |stuff13 |stuff22
aa5 |Fraya  |stuff5 |stuff14 |stuff23
aa6 |Ted    |stuff6 |stuff15 |stuff24
aa7 |Velma  |stuff7 |stuff16 |stuff25
aa8 |Carol  |stuff8 |stuff17 |stuff26
aa9 |LeRoy  |stuff9 |stuff18 |stuff27
Name             |SXH1      |Id     |SXH2
--------------------------------------------
Maya Townsend    |thing1    |mm1    |thing20
David Collste    |thing2    |mm2    |thing21
Bob              |thing3    |aa1    |thing22
Jennifer Menke   |thing4    |mm4    |thing23
Aissata Camara   |thing5    |mm5    |thing24
Marta Arranz     |thing6    |mm6    |thing25
Michael  Lennon  |thing7    |mm7    |thing26
Jonny Norton     |thing8    |mm8    |thing27
Carol            |thing9    |aa8    |thing28
Ted              |thing10   |aa6    |thing29
Katie Boone      |thing11   |mm11   |thing30
Adeline Sibanda  |thing12   |mm12   |thing31
Brian Blankinship|thing13   |mm13   |thing32
Christine Lai    |thing14   |mm14   |thing33
Jennifer Pierce  |thing15   |mm15   |thing34
Federico Bellone |thing16   |mm16   |thing35
Jeffrey Su       |thing17   |mm17   |thing36
Alice            |thing18   |aa4    |thing37
Sarah Ditton     |thing19   |mm19   |thing38
Id  |Label  |SAH1   |SAH2    |SAH3    |Name |SXH1   |SXH2  
------------------------------------------------------------
aa1 |Bob    |stuff1 |stuff10 |stuff19 |Bob  |thing3 |thing22 
aa2 |Eggbert|stuff2 |stuff11 |stuff20 |""   |""     |""
aa3 |Juan   |stuff3 |stuff12 |stuff21 |""   |""     |""
aa4 |Alice  |stuff4 |stuff13 |stuff22 |Alice|thing18|thing37
aa5 |Fraya  |stuff5 |stuff14 |stuff23 |""   |""     |""
aa6 |Ted    |stuff6 |stuff15 |stuff24 |Ted  |thing10|thing29
aa7 |Velma  |stuff7 |stuff16 |stuff25 |""   |""     |""
aa8 |Carol  |stuff8 |stuff17 |stuff26 |Carol|thing9 |thing28
aa9 |LeRoy  |stuff9 |stuff18 |stuff27 |""   |""     |""
我一直在寻找一种方法来做这件事,但却找不到任何我有能力改变的东西

这是一个谷歌测试数据表

谢谢

合并匹配行

function combineMatchingRows() {
  const ss=SpreadsheetApp.getActive();
  const sh1=ss.getSheetByName('Sheet1');
  const sh2=ss.getSheetByName('Sheet2');
  const osh=ss.getSheetByName('Sheet3');//output sheet
  const vA1=sh1.getRange(2,1,sh1.getLastRow()-1,sh1.getLastColumn()).getDisplayValues();
  const hA1=sh1.getRange(1,1,1,sh1.getLastColumn()).getDisplayValues()[0];
  var hObj1={};//columns in sheet1
  hA1.forEach((h,i)=>{hObj1[h]=i+1;});
  const vA2=sh2.getRange(2,1,sh2.getLastRow()-1,sh2.getLastColumn()).getDisplayValues();
  const hA2=sh2.getRange(1,1,1,sh2.getLastColumn()).getDisplayValues()[0];
  var hObj2={};//columns in sheet2
  hA2.forEach((h,i)=>{hObj2[h]=i+1;});
  const id2A=sh2.getRange(2,hObj2['Id'],sh2.getLastRow()-1,1).getValues().flat();//array of ids in sheet2
  var vO=[];
  vA1.forEach((r,i)=>{
    let idx=id2A.indexOf(r[hObj1['Id']-1]);
    if(idx!=-1) {
      vA2[idx].splice(hObj2['Id']-1,1);//remove second Id column
      var row=r.concat(vA2[idx]);
      vO.push(row);
    }
  });
  osh.clearContents();
  osh.getRange(1,1,vO.length,vO[0].length).setValues(vO);
}
表1:

Id,Label,SAH1,SAH2,SAH3
aa1,Bob,stuff1,stuff10,stuff19
aa2,Eggbert,stuff2,stuff11,stuff20
aa3,Juan,stuff3,stuff12,stuff21
aa4,Alice,stuff4,stuff13,stuff22
aa5,Fraya,stuff5,stuff14,stuff23
aa6,Ted,stuff6,stuff15,stuff24
aa7,Velma,stuff7,stuff16,stuff25
aa8,Carol,stuff8,stuff17,stuff26
aa9,LeRoy,stuff9,stuff18,stuff27
第2张:

Name,SXH1,Id,SXH2
Maya Townsend,thing1,mm1,thing20
David Collste,thing2,mm2,thing21
Bob,thing3,aa1,thing22
Jennifer Menke,thing4,mm4,thing23
Aissata Camara,thing5,mm5,thing24
Marta Arranz,thing6,mm6,thing25
Michael  Lennon,thing7,mm7,thing26
Jonny Norton,thing8,mm8,thing27
Carol,thing9,aa8,thing28
Ted,thing10,aa6,thing29
Katie Boone,thing11,mm11,thing30
Adeline Sibanda,thing12,mm12,thing31
Brian Blankinship,thing13,mm13,thing32
Christine Lai,thing14,mm14,thing33
Jennifer Pierce,thing15,mm15,thing34
Federico Bellone,thing16,mm16,thing35
Jeffrey Su,thing17,mm17,thing36
Alice,thing18,aa4,thing37
Sarah Ditton,thing19,mm19,thing38
第三张(输出):

合并匹配行

function combineMatchingRows() {
  const ss=SpreadsheetApp.getActive();
  const sh1=ss.getSheetByName('Sheet1');
  const sh2=ss.getSheetByName('Sheet2');
  const osh=ss.getSheetByName('Sheet3');//output sheet
  const vA1=sh1.getRange(2,1,sh1.getLastRow()-1,sh1.getLastColumn()).getDisplayValues();
  const hA1=sh1.getRange(1,1,1,sh1.getLastColumn()).getDisplayValues()[0];
  var hObj1={};//columns in sheet1
  hA1.forEach((h,i)=>{hObj1[h]=i+1;});
  const vA2=sh2.getRange(2,1,sh2.getLastRow()-1,sh2.getLastColumn()).getDisplayValues();
  const hA2=sh2.getRange(1,1,1,sh2.getLastColumn()).getDisplayValues()[0];
  var hObj2={};//columns in sheet2
  hA2.forEach((h,i)=>{hObj2[h]=i+1;});
  const id2A=sh2.getRange(2,hObj2['Id'],sh2.getLastRow()-1,1).getValues().flat();//array of ids in sheet2
  var vO=[];
  vA1.forEach((r,i)=>{
    let idx=id2A.indexOf(r[hObj1['Id']-1]);
    if(idx!=-1) {
      vA2[idx].splice(hObj2['Id']-1,1);//remove second Id column
      var row=r.concat(vA2[idx]);
      vO.push(row);
    }
  });
  osh.clearContents();
  osh.getRange(1,1,vO.length,vO[0].length).setValues(vO);
}
表1:

Id,Label,SAH1,SAH2,SAH3
aa1,Bob,stuff1,stuff10,stuff19
aa2,Eggbert,stuff2,stuff11,stuff20
aa3,Juan,stuff3,stuff12,stuff21
aa4,Alice,stuff4,stuff13,stuff22
aa5,Fraya,stuff5,stuff14,stuff23
aa6,Ted,stuff6,stuff15,stuff24
aa7,Velma,stuff7,stuff16,stuff25
aa8,Carol,stuff8,stuff17,stuff26
aa9,LeRoy,stuff9,stuff18,stuff27
第2张:

Name,SXH1,Id,SXH2
Maya Townsend,thing1,mm1,thing20
David Collste,thing2,mm2,thing21
Bob,thing3,aa1,thing22
Jennifer Menke,thing4,mm4,thing23
Aissata Camara,thing5,mm5,thing24
Marta Arranz,thing6,mm6,thing25
Michael  Lennon,thing7,mm7,thing26
Jonny Norton,thing8,mm8,thing27
Carol,thing9,aa8,thing28
Ted,thing10,aa6,thing29
Katie Boone,thing11,mm11,thing30
Adeline Sibanda,thing12,mm12,thing31
Brian Blankinship,thing13,mm13,thing32
Christine Lai,thing14,mm14,thing33
Jennifer Pierce,thing15,mm15,thing34
Federico Bellone,thing16,mm16,thing35
Jeffrey Su,thing17,mm17,thing36
Alice,thing18,aa4,thing37
Sarah Ditton,thing19,mm19,thing38
第三张(输出):


我相信你的目标和情况如下

  • 您希望使用Google Apps脚本从问题中的
    arr1
    arr2
    实现转换
    result arr
  • 您不知道
    arr2
    工作表中的
    Id
为了实现您的目标,我想提出以下流程

  • 从2个源工作表中检索值
  • 使用“Id”值从“arr2”表的值创建对象
  • 使用“arr1”和“arr2”工作表的值创建一个数组以放入结果工作表
  • 将结果表中的值放到“结果”表中
  • 当上述流程反映到脚本中时,它将变成如下所示

    示例脚本: 请设置每张图纸的名称

    function myFunction() {
      const arr1SheetName = "arr1";
      const arr2SheetName = "arr2";
      const resultSheetName = "Result";
    
      const ss = SpreadsheetApp.getActiveSpreadsheet();
    
      // 1. Retrieve values from 2 source sheets.
      const [[header1, ...values1], [header2, ...values2]] = [arr1SheetName, arr2SheetName].map(s => ss.getSheetByName(s).getDataRange().getValues());
    
      // 2. Create an object from the values of "arr2" sheet using the values of "Id".
      const indexId = header2.indexOf("Id");
      const obj = values2.reduce((o, r) => {
        const id = r[indexId];
        r.splice(indexId, 1);
        return Object.assign(o, {[id]: r});
      }, {});
    
      // 3. Create an array for putting to the result sheet using the values of "arr1" and "arr2" sheets.
      const array = values1.map(r => obj[r[0]] ? r.concat(obj[r[0]]) : r.concat(Array(3).fill("")));
      header2.splice(indexId, 1);
      array.unshift([...header1, ...header2]);
      
      // 4. Put values to the result sheet to "Result" sheet.
      const sheet = ss.getSheetByName(resultSheetName);
      sheet.clearContents();
      sheet.getRange(1, 1, array.length, array[0].length).setValues(array);
    }
    
    • 在这种情况下,从共享电子表格中,工作表名称为
      arr1
      arr2
      Result
      。请小心这个
    • 而且,在这个脚本中,即使在
      arr2
      工作表中的
      Id
      列被移动时,脚本也可以工作
    结果: 使用此脚本时,将获得以下结果

    从…起
    • arr1
      工作表

    • arr2
      工作表

    致:
    • 结果
      工作表

    参考资料:

      • 我相信你的目标和情况如下

        • 您希望使用Google Apps脚本从问题中的
          arr1
          arr2
          实现转换
          result arr
        • 您不知道
          arr2
          工作表中的
          Id
        为了实现您的目标,我想提出以下流程

      • 从2个源工作表中检索值
      • 使用“Id”值从“arr2”表的值创建对象
      • 使用“arr1”和“arr2”工作表的值创建一个数组以放入结果工作表
      • 将结果表中的值放到“结果”表中
      • 当上述流程反映到脚本中时,它将变成如下所示

        示例脚本: 请设置每张图纸的名称

        function myFunction() {
          const arr1SheetName = "arr1";
          const arr2SheetName = "arr2";
          const resultSheetName = "Result";
        
          const ss = SpreadsheetApp.getActiveSpreadsheet();
        
          // 1. Retrieve values from 2 source sheets.
          const [[header1, ...values1], [header2, ...values2]] = [arr1SheetName, arr2SheetName].map(s => ss.getSheetByName(s).getDataRange().getValues());
        
          // 2. Create an object from the values of "arr2" sheet using the values of "Id".
          const indexId = header2.indexOf("Id");
          const obj = values2.reduce((o, r) => {
            const id = r[indexId];
            r.splice(indexId, 1);
            return Object.assign(o, {[id]: r});
          }, {});
        
          // 3. Create an array for putting to the result sheet using the values of "arr1" and "arr2" sheets.
          const array = values1.map(r => obj[r[0]] ? r.concat(obj[r[0]]) : r.concat(Array(3).fill("")));
          header2.splice(indexId, 1);
          array.unshift([...header1, ...header2]);
          
          // 4. Put values to the result sheet to "Result" sheet.
          const sheet = ss.getSheetByName(resultSheetName);
          sheet.clearContents();
          sheet.getRange(1, 1, array.length, array[0].length).setValues(array);
        }
        
        • 在这种情况下,从共享电子表格中,工作表名称为
          arr1
          arr2
          Result
          。请小心这个
        • 而且,在这个脚本中,即使在
          arr2
          工作表中的
          Id
          列被移动时,脚本也可以工作
        结果: 使用此脚本时,将获得以下结果

        从…起
        • arr1
          工作表

        • arr2
          工作表

        致:
        • 结果
          工作表

        参考资料:

        “我找不到我有能力改变的东西”-所以你希望我们提出解决方案?请记住StackOverflow不是免费的编码服务。请首先尝试使用您的技能,尝试自己找到解决方案,当您在这一过程中遇到具体的编程问题时,我们可以帮助您解决。如果您不知道“id”在
        arr2
        中的位置,您如何将该值与正确的“列”相关联?例如,您如何知道
        arr2[0]
        中的哪个值对应于“SXH1”?此外,请确认
        arr1
        arr2
        之间存在哪种类型的关系(例如一对一、一对多、多对一、多对多)好吗?您计划如何处理ID不存在的行match@Cooper我需要那些附加的行值为空,只有匹配的ID才会有附加值。我回答了你的问题吗?这不是一个很难的问题,你应该自己尝试,当你遇到问题时再回来。“我找不到任何我有能力改变的东西”-所以你希望我们提出解决方案?请记住StackOverflow不是免费的编码服务。请首先尝试使用您的技能,尝试自己找到解决方案,当您在这一过程中遇到具体的编程问题时,我们可以帮助您解决。如果您不知道“id”在
        arr2
        中的位置,您如何将该值与正确的“列”相关联?例如,您如何知道
        arr2[0]
        中的哪个值对应于“SXH1”?此外,请确认
        arr1
        arr2
        之间存在哪种类型的关系(例如一对一、一对多、多对一、多对多)好吗?您计划如何处理ID不存在的行match@Cooper我需要那些附加的行值为空,只有匹配的ID才会有附加值。我回答你的问题了吗?这不是一个很难的问题,你应该自己尝试,当你有问题的时候再来。非常感谢你,这正是我需要的。我期待着研究你的答案,