我在Visual Basic for Applications中拥有多年的编程经验,但现在我正在尝试在Google Drive环境中学习自动化。感觉就像我从头开始。任何可以提供帮助的人都非常感激。
下面的脚本不起作用,因为它首先在源表(活动表)上运行saveAsSpreadsheet,然后还在源表(活动表)上运行copyValuesToRange。我想在第一个命令saveAsSpreadsheet生成的NEW工作表上运行copyValuesToRange。
如何引用新生成的工作表?(在我的示例中,它被称为NewDocName,它是从源表中的命名范围中提取的值)
谢谢!
-CP
function BackupPayroll(){
saveAsSpreadsheet();
copyValuesToRange();
}
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var NewDocName = sheet.getRangeByName('Backup').getValues();
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxx");
DriveApp.getFileById(sheet.getId()).makeCopy(NewDocName, destFolder);
}
function copyValuesToRange(sheet, column, columnEnd, row, rowEnd) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheets()[3];
var destination = ss.getSheets()[3];
var range = source.getRange("A1:P200");
range.copyValuesToRange(destination, 1, 16, 1, 200);
}
答案 0 :(得分:1)
以下样本怎么样?
它会在copyValuesToRange()
返回新电子表格的ID,而function BackupPayroll(){
var id = saveAsSpreadsheet();
copyValuesToRange(id, null, null, null, null, null);
}
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var NewDocName = sheet.getRangeByName('Backup').getValues();
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxx");
var newsheet = DriveApp.getFileById(sheet.getId()).makeCopy(NewDocName, destFolder);
return newsheet.getId();
}
function copyValuesToRange(id, sheet, column, columnEnd, row, rowEnd) {
var ss = SpreadsheetApp.openById(id);
var source = ss.getSheets()[3];
var destination = ss.getSheets()[3];
var range = source.getRange("A1:P200");
range.copyValuesToRange(destination, 1, 16, 1, 200);
}
则会使用该ID打开新的电子表格。
position
如果我误解了你的问题,我很抱歉。