【问题标题】:Google Apps Script Inner Join RangesGoogle Apps 脚本内部连接范围
【发布时间】:2015-04-02 23:21:40
【问题描述】:

我想通过“ID”“内连接”2 个数据范围(SHEET 1 和 2)并将其结果输出到 SHEET 3(如下所示)

对于所有工作表,第一行只是标题,实际值从第二行开始。

工作表 1 是整个数据库(3000 行),工作表 2(1000 行)是工作表 1 的选择以及附加数据。每个条目 (ID) 在每个工作表中仅出现一次。

Sheet 3 应仅显示 Sheet 2 的行,但包含 Sheet 1 的相应行(按 ID)的数据。

var sheetOneVals = sheetOne.getDataRange().getValues(); sheetOneVals.splice(0, 1);

var sheetTwoVals = sheetTwo.getDataRange().getValues(); sheetTwoVals.splice(0, 1);

如何做到这一点? https://stackoverflow.com/a/17500836/379777 几乎是我想要的,只是它使用了我没有的密钥。

表 1:

     A         B           C      D
  ------------------------------------
1 | Name | Description | Price | ID  |
  ------------------------------------
2 | ABC  | Bla1        | 10    | 123 |
  ------------------------------------
3 | DEF  | Bla2        | 8     | 234 |
  ------------------------------------  
: | :    | :           | :     | :   |
: | :    | :           | :     | :   |
  ------------------------------------          

表 2:

    A     B      C     D         E
  ---------------------------------------
1 | ID  | Cat1 | Cat2 | Cat3 | Nonsense |
  ---------------------------------------   
2 | 123 | B    | C    | D    | xyz      |
  ---------------------------------------
: | :   | :    | :    | :    | :        |
: | :   | :    | :    | :    | :        |
  ---------------------------------------   

表 3(所需结果):

     A       B      C     D       E         F         G
  ----------------------------------------------------------
1 | ID   | Cat1 | Cat2 | Cat3 | Name | Description | Price |
  ----------------------------------------------------------    
2 | 123  | B    | C    | D    | ABC  | Bla1        | 10    |
  ----------------------------------------------------------
: | :    | :    | :    | :    | :    | :           | :     |
: | :    | :    | :    | :    | :    | :           | :     |
  ----------------------------------------------------------

【问题讨论】:

    标签: javascript google-apps-script


    【解决方案1】:

    您只想要逻辑还是要有人编写代码?简单快速的逻辑是:

    开始和对象,以 Key 作为 ID。
    遍历工作表 1 并将所有值保存为这些 ID 中的对象。
    迭代槽表 2,在 ID 中再次保存新键。
    将对象解析为双精度数组。

    这是一些存根:

    Initiate myObject
    Get Sheet1 and sheet2
    for( lines in sheet 1 )
      myObject[ lineId ] = {}
        for( cols in sheet 1[lines] )
          myObject[ lineId ][ columnHeading ] = columnValue
    
    for( lines in sheet 2 )
      for( cols in sheet 1[lines] )
        myObject[ lineId ][ columnHeading ] = columnValue
    

    您最终会得到一个重复的 ID,作为属性键和其中的属性值,但没什么大不了的。

    Initiate arrayToPaste
    Initiate currLine with 0
    for( props in myObject )
      Initiate first array key as an Array
      for( keys in myObject[ props ] ){
        arrayToPaste[ currLine ].push(myObject[ props ][ key ])
      }
      Increase currLine
    
    Paste arrayToPaste to sheet
    

    只需在某处插入Heading,可以在Initialization中,在第一个for之后,在粘贴时,作为不同的数组等等...

    我还有一个问题,为什么是 parseInt?

    【讨论】:

    • 感谢您的回答。你是对的, parseInt 没用。我删除了它。但是,我认为您的解决方案不能解决问题。我编辑了文本,也许现在我想要的更清楚了。
    • 是的,它会的。所有值都将保存在一个对象中,以 id 作为键,直接构造对象看起来像这样:myObject = { 123:{Cat1:'B',Cat2:'C':Cat3:'D',Nonsense:'xyz',Description:'Bla1' ,Price:10,ID:123,Name:'ABC' }, 234{same as 123 with differente values}};,这非常适合解析为双精度数组,其中 123 和 234 是行(不是文字行号),其中的每个键都是一列。
    • 逻辑已经编码在链接的s.o中。在问题中。
    【解决方案2】:

    我最终得到了以下解决方案。我可能不够好,无法完全理解您的解决方案。我相信您的解决方案要快得多。

    function createSheet3(){
      var ss        = SpreadsheetApp.getActiveSpreadsheet();
      var sheet1 = ss.getSheetByName("sheet1");
      var sheet2 = ss.getSheetByName("sheet2");
      var sheet3 = ss.getSheetByName("sheet3");
    
      var sheet3Header = [Name,Description,Price,ID,ID,Cat1,Cat2,Cat3,Nonsense]
    
      var sheetOneVals = sheet1.getDataRange().getValues();
      sheetOneVals.splice(0, 1);
    
      var sheetTwoVals = sheet2.getDataRange().getValues();
      sheetTwoVals.splice(0, 1);
    
      var consolidatedArr = new Array();
    
      for each (var item in sheetTwoVals){  
       for (var i in sheetOneVals) {
         if(item[0] == sheetOneVals[i][3]){
           consolidatedArr.push(sheetOneVals[i].concat(item));
         }
        }
      }
      sheet3.clear({contentsOnly:true});
      sheet3.getRange(1,1,1,9).setValues(sheet3Header);
      sheet3.getRange(2,1,consolidatedArr.length,9).setValues(consolidatedArr);
    
    
      SpreadsheetApp.flush();                          
      Utilities.sleep("200"); 
      if(consolidatedArr.length > 0){
        var last = sheet3.getLastRow();                     
        var max  = sheet3.getMaxRows();
        if (last !== max && max-last > 1) {sheet3.deleteRows(last+2,max-last-1);}
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 2014-10-05
      相关资源
      最近更新 更多