[英]Copying part of a row from one Google spreadsheet to another google spreadsheet based on a value in a cell?
I am trying to write a script to copy column B:K of a specific row based on if column 11 (K) has a "Y" on it.我正在尝试编写一个脚本来根据第 11 (K) 列是否有“Y”来复制特定行的 B:K 列。
I have scripts that move from one sheet to another within the same spreadsheet that work just fine but I am really struggling with one spreadsheet to another and only moving part of the row instead of the whole thing.我有脚本可以在同一个电子表格中从一张工作表移动到另一张工作表,但我真的很努力将一个电子表格移到另一个电子表格,并且只移动行的一部分而不是整个事情。
Below is the script thus far... would love help!以下是迄今为止的脚本......希望得到帮助!
function onEdit() {
var sss = SpreadsheetApp.openById("15RWoaW8tRLVGOTDrZvBLJwx0beWEsV3x8pfwGe2zMfc").getSheetByName("2018 2 Week Snapshot for Jim"); // sss = source spreadsheet
var r = sss.getActiveRange();
if(sss.getName() == "2018 2 Week Snapshot for Jim" && r.getColumn() == 11 && r.getValue() == "Y") {
var row = r.getRow();
var numColumns = sss.getLastColumn();
var tss = SpreadsheetApp.openById("1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So"); // tss = target spreadsheet
var targetSheet = tss.getSheetByName("Test Sheet"); // ts = target sheet
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
tss.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
This is my new code, still struggling:这是我的新代码,仍在挣扎:
function onEdit() {
var sss = SpreadsheetApp.openById('15RWoaW8tRLVGOTDrZvBLJwx0beWEsV3x8pfwGe2zMfc'); // sss = source spreadsheet
var ss = sss.getSheetByName('2018 2 Week Snapshot for Jim'); // ss = source sheet
var SRange = ss.getRange("C:K");
if(ss.getName() == "2018 2 Week Snapshot for Jim" && SRange.getColumn() == 9 && SRange.getValue() == "y") {
var row = SRange.getRow();
var numColumns = ss.getLastColumn();
var tss = SpreadsheetApp.openById('1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So'); // tss = target spreadsheet
var ts = tss.getSheetByName('Test Sheet'); // ts = target sheet
var target = ts.getRange(ts.getLastRow() + 1, 1);
ss.getRange(row, 1, 1, numColumns).SetValues(target);
}
}
y
or Y
to column K
at the sheet of 2018 2 Week Snapshot for Jim
on the active spreadsheet, you want to copy only the values of B:K
to the last row at the sheet of "Test Sheet" on the spreadsheet of 1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So
.y
或Y
放在活动电子表格上2018 2 Week Snapshot for Jim
的2018 2 Week Snapshot for Jim
表的K
列时,您只想将B:K
的值复制到电子表格上“测试表”工作表的最后一行1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So
。
15RWoaW8tRLVGOTDrZvBLJwx0beWEsV3x8pfwGe2zMfc
is the active spreadsheet. 15RWoaW8tRLVGOTDrZvBLJwx0beWEsV3x8pfwGe2zMfc
的电子表格是活动电子表格。 If my understanding is correct, how about this modification?如果我的理解是正确的,这个修改怎么样?
set
of setValues()
is lower case. setValues()
set
是小写的。values
of setValues(values)
is 2 dimentional array. values
的setValues(values)
是2维阵列。
setValues(values)
is the range for putting the values. setValues(values)
的范围是放置值的范围。onEdit()
, you can use an event object.onEdit()
,您可以使用事件对象。 The script which reflected above is as follows.上面反映的脚本如下。
function myFunction(e) {
var sourceSheetName = "2018 2 Week Snapshot for Jim";
var destinationSpreadsheetId = "1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So";
var destinationSheetName = "Test Sheet";
if (e.source.getSheetName() == sourceSheetName && e.range.getA1Notation()[0] == "K" && e.value.toUpperCase() == "Y") {
var sourceValues = e.source.getRange("B:K").getValues().filter(function(e){return e.filter(String).length > 0});
var tss = SpreadsheetApp.openById(destinationSpreadsheetId);
var ts = tss.getSheetByName(destinationSheetName);
ts.getRange(ts.getLastRow() + 1, 1, sourceValues.length, sourceValues[0].length).setValues(sourceValues);
}
}
myFunction()
as an installable trigger.myFunction()
作为可安装的触发器。 If I misunderstand your situation, I'm sorry.如果我误解了你的情况,我很抱歉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.