[英]Pull data from one Google spreadsheet to another using Google script
I have 2 spreadsheet in my Drive. 我的云端硬盘中有2个电子表格。 I want to pull data from a cell in 1 spreadsheet and copy it in another. 我想从1个电子表格的单元格中提取数据,然后将其复制到另一个电子表格中。 The spreadsheet "TestUsage" will sometimes have data in column A, but none is column B. I would like so that when I open the spreadsheet, it would populate that empty cell in sheet "TestUsage" from sheet "TestParts". 电子表格“ TestUsage”有时会在A列中包含数据,但B列中没有。在我打开电子表格时,它将在工作表“ TestParts”中的工作表“ TestUsage”中填充该空白单元格。 Here is my code: 这是我的代码:
var ssTestUsage = SpreadsheetApp.getActiveSpreadsheet();
var sTestUsage = ssTestUsage.getActiveSheet();
var lastRowTestUsage = sTestUsage.getLastRow();
var rangeTestUsage = sTestUsage.getSheetValues(1, 1, lastRowTestUsage, 4);
var TESTPARTS_ID = "1NjaFo0Y_MR2uvwit1WuNeRfc7JCOyukaKZhuraWNmKo";
var ssTestParts = SpreadsheetApp.openById(TESTPARTS_ID);
var sTestParts = ssTestParts.getSheets()[0];
var lastRowTestParts = sTestParts.getLastRow();
var rangeTestParts = sTestParts.getSheetValues(1, 1, lastRowTestParts, 3);
function onOpen() {
for (i = 2; i < lastRowTestUsage; i++) {
if (rangeTestUsage[i][0] !== "" && rangeTestUsage[i][1] == "") {
for (j = 1; j < lastRowTestParts; j++) {
if (rangeTestUsage[i][0] == rangeTestParts[j][0]) {
Logger.log(rangeTestUsage[i][1]);
Logger.log(rangeTestParts[j][1]);
rangeTestUsage[i][1] = rangeTestParts[j][1];
break;
}
}
}
}
}
The problem with this is this doesn't do anything: 这个问题是什么都不做:
rangeTestUsage[i][1] = rangeTestParts[j][1];
I know there must be a method that can get data from one range to another. 我知道必须有一种方法可以从一个范围到另一个范围获取数据。 Please let me know if I am totally incorrect or I am on the right path. 如果我完全不正确,或者我走的路正确,请告诉我。
the statement 该声明
"this doesn't do anything:" “这没有做任何事情:”
rangeTestUsage[i][1] = rangeTestParts[j][2];
is not really true... and not really false neither..., actually it does assign the value to rangeTestUsage i but you dont see it because it is not reflected in the spreadsheet. 不是真的,也不是真的...,实际上它确实将值分配给rangeTestUsage i,但是您没有看到它,因为它没有反映在电子表格中。
Both values are taken from the Sheet using getValues
so at that time they are both array elements. 这两个值都是使用getValues
从Sheet中获取的,因此当时它们都是数组元素。
What is missing is just writing back the array to the sheet using the symetrical statement setValues()
缺少的只是使用对称语句setValues()
将数组写回到工作表
Give it a try and don't hesitate to come back if something goes wrong. 尝试一下,如果出现问题,请立即返回。
EDIT : 编辑:
I didn't notice at first that you were using getSheetValues
instead of getValues
(simply because I never use this one)... the only difference is that getValues is a method of the range class while yours belongs to the sheet class ; 起初我没有注意到您使用的是getSheetValues
而不是getValues
(这是因为我从未使用过它)……唯一的区别是getValues是range类的一种方法,而您的属于sheet类 ; the syntax is similar in a way, just use 语法在某种程度上相似,只是使用
Sheet.getRange(row,col,width,height).getValues()
it takes one word more but has the advantage to have a direct equivalent to set values 它需要多花一个字,但具有直接等于设定值的优点
Sheet.getRange(row,col,width,height).setValues()
Serge insas has a good explanation of why your code doesn't work and hints at the solution below. Serge insas很好地解释了为什么您的代码不起作用,并提示了以下解决方案。
I recommend you use an array to store the updated values of column B that you want then set the entire column at the end. 我建议您使用一个数组来存储所需的列B的更新值,然后在末尾设置整个列。
var ssTestUsage = SpreadsheetApp.getActiveSpreadsheet();
var sTestUsage = ssTestUsage.getActiveSheet();
var lastRowTestUsage = sTestUsage.getLastRow();
var rangeTestUsage = sTestUsage.getSheetValues(1, 1, lastRowTestUsage, 2);
var TESTPARTS_ID = "1NjaFo0Y_MR2uvwit1WuNeRfc7JCOyukaKZhuraWNmKo";
var ssTestParts = SpreadsheetApp.openById(TESTPARTS_ID);
var sTestParts = ssTestParts.getSheets()[0];
var lastRowTestParts = sTestParts.getLastRow();
var rangeTestParts = sTestParts.getSheetValues(1, 1, lastRowTestParts, 2);
var colB = [];
function onOpen() {
for (i = 2; i < lastRowTestUsage; i++) {
if (rangeTestUsage[i][0] !== "" && rangeTestUsage[i][1] == "") {
var matched = false;
for (j = 1; j < lastRowTestParts; j++) {
if (rangeTestUsage[i][0] == rangeTestParts[j][0]) {
//Logger.log(rangeTestUsage[i][1]);
//Logger.log(rangeTestParts[j][1]);
colB.push([rangeTestParts[j][1]]); // push the value we want into colB array
matched = true;
break;
}
}
if(!matched) // this is in case you don't have a match
colB.push([""]); // incase we don't have a matching part
} else {
colB.push([rangeTestUsage[i][0]]); // we already have a value we want so just add that to colB array
}
}
sTestUsage.getRange(2,2,lastRowTestUsage).setValues(colB); // update entire column b with values in colB array
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.