[英]Copy Data from One Google sheets Workbook Sheet to another Workbook
So very much piecing together and learning google scripts and 1st challenge is to do the following: Several subsheets completed by agents who will use a script to collate onto a master sheet any that are not already on there.所以非常多地拼凑和学习谷歌脚本,第一个挑战是执行以下操作:由代理完成的几个子表,他们将使用脚本将任何尚未存在的主表整理到主表上。
Script will need to: Check if data in the subsheet table has been been already copied to master sheet (look at a cell for a 0 or 1).脚本需要: 检查子表中的数据是否已复制到主表(查看单元格中的 0 或 1)。 If 0 copy that to the last row of the master sheet.如果为 0,则将其复制到主工作表的最后一行。 I've used a countifs formula to populate the 0 or 1 on each sub sheet.我使用 countifs 公式填充每个子表上的 0 或 1。
I've managed to build the script to do this from one sheet to another in the same workbook but get an error message when trying to do so in different workbooks.我已设法构建脚本以在同一工作簿中从一张工作表到另一张工作表执行此操作,但在尝试在不同工作簿中执行此操作时收到错误消息。 Also found a clone script that does copy the data (whole page) to the master but overwrites and not last row and also ignores the 0 and 1 rule.还发现了一个克隆脚本,它确实将数据(整个页面)复制到主服务器,但覆盖而不是最后一行,并且也忽略了 0 和 1 规则。
Looked across many similar questions and online and struggling to see anyone with this same need and fix?浏览了许多类似的问题和在线并努力寻找有同样需求和解决方案的人?
Codes: Copy all the new data to another sheet in same workbook from the last row: /**代码:将所有新数据从最后一行复制到同一工作簿中的另一张工作表:/**
Retrieves all the rows in the active spreadsheet that contain Yes检索活动电子表格中包含 Yes 的所有行
in the Include column and copies them to the Report sheet.在“包括”列中并将它们复制到“报告”表中。 */ */
function copyRows2() { function copyRows2() {
var sSheet = SpreadsheetApp.getActiveSpreadsheet(); var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("Import2"); var srcSheet = sSheet.getSheetByName("Import2");
var tarSheet = sSheet.getSheetByName("Report"); var tarSheet = sSheet.getSheetByName("报告");
var lastRow = srcSheet.getLastRow(); var lastRow = srcSheet.getLastRow();
for (var i = 2; i <= lastRow; i++) { var cell = srcSheet.getRange("C" + i); for (var i = 2; i <= lastRow; i++) { var cell = srcSheet.getRange("C" + i); var val = cell.getValue(); var val = cell.getValue(); if (val == "0") {如果(val ==“0”){
var srcRange = srcSheet.getRange("A" + i + ":D" + i); var srcRange = srcSheet.getRange("A" + i + ":D" + i);
var tarRow = tarSheet.getLastRow(); var tarRow = tarSheet.getLastRow(); var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":D" + (tarRow+1)); var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":D" + (tarRow+1));
srcRange.setValues(tarRange); srcRange.setValues(tarRange); } } };` } } };`
Clone whole Sheet from workbook A to workbook B:将整个工作表从工作簿 A 克隆到工作簿 B:
function cloneGoogleSheet(ssA, ssB) {
// source doc
var sss = SpreadsheetApp.openById('Source Workbook ID');
// source sheet
var ss = sss.getSheetByName('Import');
// Get full range of data
var SRange = ss.getDataRange();
// get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
// get the data values in range
var SData = SRange.getValues();
// target spreadsheet
var tss = SpreadsheetApp.openById('Target WorkbookID');
// target sheet
var ts = tss.getSheetByName('Master2');
// set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
};
Update: have a working code now of:更新:现在有一个工作代码:
/**
* Retrieves all the rows in the active spreadsheet that contain Yes
* in the Include column and copies them to the Report sheet.
*/
function MasterTracker() {
var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("DARS");
var tSheet = SpreadsheetApp.openById('DOC ID');
var tarSheet = tSheet.getSheetByName("TrackerPilot");
var lastRow = srcSheet.getLastRow();
for (var i = 2; i <= lastRow; i++) {
var cell = srcSheet.getRange("A" + i);
var val = cell.getValue();
if (val == "0") {
var srcRange = srcSheet.getRange("E" + i + ":U" + i);
var tarRow = tarSheet.getLastRow();
var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":Q" + (tarRow+1));
var sourceValues = srcRange.getValues();
tarRange.setValues(sourceValues);
}
}
};
But a new problem.而是一个新的问题。 On the source sheet there are cells with dropdowns from data validation - even when blank these delay the code when running.在源表上,有一些带有数据验证下拉列表的单元格——即使是空白的,这些单元格也会在运行时延迟代码。 Is there an easy way I am missing from Ignoring these rows if the lookup Cell is blank (not a 0 or 1)?如果查找单元格为空白(不是 0 或 1),是否有一种简单的方法可以忽略这些行?
function cloneGoogleSheet(ssA, ssB) {
// source doc
var sss = SpreadsheetApp.openById('Source Workbook ID');
// source sheet
var ss = sss.getSheetByName('Import');
// Get full range of data
var SRange = ss.getDataRange();
// get the data values in range
var SData = SRange.getValues();
var i = 0;
var TData = [];
for( i=0; i<SData.length; i++ ) {
if( SData[i][2] === 0 ) { // Column C = 0
TData.push(SData[i]);
}
}
// target spreadsheet
var tss = SpreadsheetApp.openById('Target WorkbookID');
// target sheet
var ts = tss.getSheetByName('Master2');
// set the target range to the values of the source data
ts.getRange(1,1,TData.length,TData[0].length).setValues(TData); // Over write
// Append ts.getRange(ts.getLastRow()+1,1,TData.length,TData[0].length).setValues(TData);
};
I haven't been able to test it but I'm pretty sure it will work.我无法对其进行测试,但我很确定它会起作用。
/**
* Retrieves all the rows in the active spreadsheet that contain Yes
* in the Include column and copies them to the Report sheet.
*/
function MasterTracker() {
var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("DARS");
var tSheet = SpreadsheetApp.openById('DOC ID');
var tarSheet = tSheet.getSheetByName("TrackerPilot");
var srcData = srcSheet.getDataRange().getValues(); // get all values
var sourceValues = [];
for (var i=1; i<=srcSheet.getLastRow(); i++) {
// i+1 is row number i is index in data array
// A is column 1 so index is 0 in data array
if ( srcData[i][0] === 0 ) { // assuming the value is a number?
sourceValues.push(srcData[i].slice(4,21)); // Column E is index 4 and V (Not included) is index 21
}
}
tarSheet.getRange(tarSheet.getLastRow()+1,1,sourceValues.length,sourceValues[0].length).setValues(sourceValues);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.