2015-04-02 58 views
0

我想“内部连接” 2数据范围(SHEET 1 & 2)由“ID”和输出其结果在表3(如下所示)谷歌Apps脚本内部联接范围

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

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

表3应该显示仅包括表2,但与对应的行的表中的数据(由ID)1.

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

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

如何可以这样做的行? https://stackoverflow.com/a/17500836/379777几乎是我想要的,只是它使用了Keys,我没有这种情况。

表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 | 
    ---------------------------------------------------------- 
: | : | : | : | : | : | :   | :  | 
: | : | : | : | : | : | :   | :  | 
    ---------------------------------------------------------- 

回答

0

我结束了以下解决方案。我可能不足以完全理解您的解决方案。我相信你的解决方案要快得多。

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);} 
    } 
} 
1

你只想逻辑或某人编写这一切?可以轻松快速工作的逻辑是:

开始和对象,以密钥作为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 

你会用duplica 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 

只需插入标题的地方,可以在初始化,先为,粘贴的时候,作为一个不同的阵列,等...

而且我也有一个问题,为什么parseInt函数后?

+0

谢谢你的回答。你说得对,parseInt没用。我删除它。但是,我认为您的解决方案不能解决问题。我编辑了文本,也许它更清楚我现在想要的。 – user3797772 2015-04-02 13:37:03

+0

是的,它会的。所有的值将被保存在一个对象中,以id为键,直接构造对象看起来像这样:'myObject = {123:{Cat1:'B',Cat2:'C':Cat3:'D' ,废话:'xyz',描述:'Bla1',价格:10,ID:123,名称:'ABC'},234 {与不同值的123相同}};',这非常适合解析为双数组,其中123和234是行(不是文字行号),并且其中的每个关键词都在列中。 – Kriggs 2015-04-02 13:42:32

+0

该逻辑已经在链接的s.o中编码。在问题中。 – 2015-04-02 14:57:12