[英]Google Apps Script Inner Join Ranges
I would like to "inner join" 2 data-ranges (SHEET 1 & 2) by "ID" and output its result in SHEET 3 (as shown below) 我想通过“ ID”“内部加入” 2个数据范围(SHEET 1和2),并在SHEET 3中输出其结果(如下所示)
For all sheets the first row is only the header, the actual values start from the second row. 对于所有工作表,第一行只是标题,实际值从第二行开始。
Sheet 1 is the entire Database (3000 rows), and Sheet 2 (1000 rows) is a selection of Sheet 1 with additional data. 工作表1是整个数据库(3000行),工作表2(1000行)是具有附加数据的工作表1的选择。 Each entry (ID) is only present once in each Sheet.
每个条目(ID)在每个工作表中仅出现一次。
Sheet 3 should show comprise only the rows of Sheet 2 but with the data of the corresponding row (by ID) of Sheet 1. 工作表3应该仅包含工作表2的行,但应包含工作表1的相应行(按ID)的数据。
var sheetOneVals = sheetOne.getDataRange().getValues(); sheetOneVals.splice(0, 1);
var sheetTwoVals = sheetTwo.getDataRange().getValues(); sheetTwoVals.splice(0, 1);
How can this be done? 如何才能做到这一点? https://stackoverflow.com/a/17500836/379777 was almost what I wanted except that it uses Keys, which I don't have in my case.
我几乎想要的是https://stackoverflow.com/a/17500836/379777 ,但它使用的是Keys,在我的情况下我没有。
SHEET 1: 清单1:
A B C D
------------------------------------
1 | Name | Description | Price | ID |
------------------------------------
2 | ABC | Bla1 | 10 | 123 |
------------------------------------
3 | DEF | Bla2 | 8 | 234 |
------------------------------------
: | : | : | : | : |
: | : | : | : | : |
------------------------------------
SHEET 2: 表格2:
A B C D E
---------------------------------------
1 | ID | Cat1 | Cat2 | Cat3 | Nonsense |
---------------------------------------
2 | 123 | B | C | D | xyz |
---------------------------------------
: | : | : | : | : | : |
: | : | : | : | : | : |
---------------------------------------
SHEET 3 (desired result): 表格3(预期结果):
A B C D E F G
----------------------------------------------------------
1 | ID | Cat1 | Cat2 | Cat3 | Name | Description | Price |
----------------------------------------------------------
2 | 123 | B | C | D | ABC | Bla1 | 10 |
----------------------------------------------------------
: | : | : | : | : | : | : | : |
: | : | : | : | : | : | : | : |
----------------------------------------------------------
You want just the logic or someone to code it all? 您只需要逻辑还是有人来编写所有代码? The logic that would work easy and fast is:
容易而快速地工作的逻辑是:
Start and object, with the Key as the ID. 以Key为ID的Start和object。
Iterate trough sheet 1 and save all values as objects in those IDs. 迭代槽板1并将所有值另存为这些ID中的对象。
Iterate trough sheet 2 saving again new keys, in the ID. 迭代槽板2,再次在ID中保存新密钥。
Parse the object to a Double Array. 将对象解析为Double Array。
Here's some stub: 这是一些存根:
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
You'll end up with a duplica ID, as the property key and a property in it with the value, but no biggie. 您最终将得到一个副本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
Just insert the Heading somewhere, can be in the Initialization, after the first for, when pasting, as a different array, etc... 只需将Heading插入某处,可以在Initialization中,在第一个for之后,粘贴时,作为其他数组,等等。
And I also have a question, why the parseInt? 我还有一个问题,为什么要使用parseInt?
I ended up with the following solution. 我得到了以下解决方案。 I am probably not good enough to entirely understand your solution.
我可能不足以完全了解您的解决方案。 I am sure that your solution is much faster.
我相信您的解决方案会更快。
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);}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.