Google 脚本功能 - 复制粘贴
Google Script Function - Copy Paste
我正在通过 Google 脚本编写有关单击按钮功能的脚本。我想要发生的是 SHEET 1 Values 被复制到 SHEET 2 AS VALUES(不复制 Google Sheets Formulas),然后 SHEET 1 VALUES 将被清除。但是,我似乎遇到了将值复制到 SHEET 2.
的问题
我试图搜索可以解决此问题的方法,但在编写脚本方面我并不是真正的专家,因为我是这方面的新手。
// Display a dialog box with a message and "Yes" and "No" buttons.
var ui = SpreadsheetApp.getUi();
var response = ui.alert("Do you want to capture all data?", ui.ButtonSet.YES_NO);
// Process the user's response.
if (response == ui.Button.YES) {
}
function remove() {
var spreadsheet = SpreadsheetApp.getActive().getSheetByName("2019")
var destsheet = SpreadsheetApp.getActive().getSheetByName("Handled Tickets");
var getLastContentRow = spreadsheet.getRange("A8:I").getValues();
var destination = destsheet.getRange(destsheet.getLastRow()+1,1);
var source = spreadsheet.getRange("A8:I").getValues();
getLastContentRow.copyTo(destination.CopyPastType.PASTE_VALUES);
spreadsheet.getRange('C8:E').clearContent()
spreadsheet.getRange('F8:H').clearContent()
}
预期流程:1) 单击按钮后,电子表格中的任何数据都将复制到 destsheet。 2) 复制后,电子表格中的数据将被清除。
附加规则:1)复制到destsheet后,再次点击按钮时数据不会被其他值覆盖。相反,它将查找最后一行(空单元格)并将数据复制到那里。 2) 如果所有的单元格都被使用了,会自动添加额外的100行。
错误:
在对象中找不到函数 copyTo
您上面的代码存在几个问题(语法、格式、结构、缺少结束语句的分号,...)。
假设只有 remove()
函数有问题,下面是我的版本,有几条评论。
您可能还想查看上面 UI 的部分(例如,将其嵌入到您的按钮将调用的函数中,确保您的 if
语句中有一些代码,。 ..).
function remove() {
var source_sheet = SpreadsheetApp.getActive().getSheetByName("2019"); // better not use "spreadsheet" as variable name here, this is confusing, your content is a sheet
var dest_sheet = SpreadsheetApp.getActive().getSheetByName("Handled Tickets");
var getLastContentRow = source_sheet.getRange("A8:I"); // The "copyTo" method applies to ranges, not to arrays, so remove the ".getValues()"
// --> the "getLastRow" variable name makes me believe you're only looking at copying the last row, but your current range will copy all rows starting at 8.
// --> as the same content is captured in "source" below, this might just be a misleading variable name, though, in which case you may want to simply rename it
var destination = dest_sheet.getRange(dest_sheet.getLastRow()+1,1);
// var source = spreadsheet.getRange("A8:I").getValues();
// --> this is duplicate with getLastContentRow, and not use in your function, so presumed useless. Can be removed.
getLastContentRow.copyTo(destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
// --> the example in the documentation is misleading, but this function requires a third argument for "transposed"
// spreadsheet.getRange('C8:E').clearContent()
// spreadsheet.getRange('F8:H').clearContent()
// --> why two different calls instead of 1 on C8:H directly?
// --> also, why not the same range as the one copied?
getLastContentRow.clearContent(); // This will remove all the copied content from the "source_sheet"
}
我正在通过 Google 脚本编写有关单击按钮功能的脚本。我想要发生的是 SHEET 1 Values 被复制到 SHEET 2 AS VALUES(不复制 Google Sheets Formulas),然后 SHEET 1 VALUES 将被清除。但是,我似乎遇到了将值复制到 SHEET 2.
的问题我试图搜索可以解决此问题的方法,但在编写脚本方面我并不是真正的专家,因为我是这方面的新手。
// Display a dialog box with a message and "Yes" and "No" buttons.
var ui = SpreadsheetApp.getUi();
var response = ui.alert("Do you want to capture all data?", ui.ButtonSet.YES_NO);
// Process the user's response.
if (response == ui.Button.YES) {
}
function remove() {
var spreadsheet = SpreadsheetApp.getActive().getSheetByName("2019")
var destsheet = SpreadsheetApp.getActive().getSheetByName("Handled Tickets");
var getLastContentRow = spreadsheet.getRange("A8:I").getValues();
var destination = destsheet.getRange(destsheet.getLastRow()+1,1);
var source = spreadsheet.getRange("A8:I").getValues();
getLastContentRow.copyTo(destination.CopyPastType.PASTE_VALUES);
spreadsheet.getRange('C8:E').clearContent()
spreadsheet.getRange('F8:H').clearContent()
}
预期流程:1) 单击按钮后,电子表格中的任何数据都将复制到 destsheet。 2) 复制后,电子表格中的数据将被清除。
附加规则:1)复制到destsheet后,再次点击按钮时数据不会被其他值覆盖。相反,它将查找最后一行(空单元格)并将数据复制到那里。 2) 如果所有的单元格都被使用了,会自动添加额外的100行。
错误: 在对象中找不到函数 copyTo
您上面的代码存在几个问题(语法、格式、结构、缺少结束语句的分号,...)。
假设只有 remove()
函数有问题,下面是我的版本,有几条评论。
您可能还想查看上面 UI 的部分(例如,将其嵌入到您的按钮将调用的函数中,确保您的 if
语句中有一些代码,。 ..).
function remove() {
var source_sheet = SpreadsheetApp.getActive().getSheetByName("2019"); // better not use "spreadsheet" as variable name here, this is confusing, your content is a sheet
var dest_sheet = SpreadsheetApp.getActive().getSheetByName("Handled Tickets");
var getLastContentRow = source_sheet.getRange("A8:I"); // The "copyTo" method applies to ranges, not to arrays, so remove the ".getValues()"
// --> the "getLastRow" variable name makes me believe you're only looking at copying the last row, but your current range will copy all rows starting at 8.
// --> as the same content is captured in "source" below, this might just be a misleading variable name, though, in which case you may want to simply rename it
var destination = dest_sheet.getRange(dest_sheet.getLastRow()+1,1);
// var source = spreadsheet.getRange("A8:I").getValues();
// --> this is duplicate with getLastContentRow, and not use in your function, so presumed useless. Can be removed.
getLastContentRow.copyTo(destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
// --> the example in the documentation is misleading, but this function requires a third argument for "transposed"
// spreadsheet.getRange('C8:E').clearContent()
// spreadsheet.getRange('F8:H').clearContent()
// --> why two different calls instead of 1 on C8:H directly?
// --> also, why not the same range as the one copied?
getLastContentRow.clearContent(); // This will remove all the copied content from the "source_sheet"
}