【问题标题】:Google sheets cut and paste script谷歌表格剪切和粘贴脚本
【发布时间】:2020-05-01 00:33:05
【问题描述】:
全部。
看到很多剪切和粘贴 Google 表格脚本,由于copy from source + paste in destination + delete source 和cut source + paste in destination 之间的差异,所有这些脚本都从根本上失败了。不同之处在于粘贴的内容引用了与原始内容相同的单元格。在同一张表中复制会导致在单元格内复制公式并最终增加其非固定单元格引用的行和列。将复制的内容粘贴到另一张纸上会导致上述两种操作之间的另一个主要区别 - 粘贴的单元格的公式引用来自目标工作表而不是来自源工作表的值。
在 Google 表格中将Range 剪切和粘贴为脚本的正确方法是什么?
我只看到this 是一个可行的答案,但它是一个网络请求,而不是谷歌表格脚本。可能可以从表格脚本发送请求,但大概应该有一种更集成的方式来进行剪切和粘贴。可能 HTTP 请求方式是针对外部应用程序的。
【问题讨论】:
标签:
google-apps-script
google-sheets
cut-and-paste
【解决方案1】:
在 Google Apps 脚本上,您可以通过高级服务使用 Sheets API。要启用它,请从脚本编辑器菜单中单击Resources>Advanced Google services...>Google Sheets API。
一旦激活它,您就可以使用列出的here 相同的端点。
示例
let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheetId_1 = ss.getSheetByName("source").getSheetId();
let sheetId_2 = ss.getSheetByName("destination).getSheetId();
// Using the Sheets API to create the request body
let request = Sheets.newCutPasteRequest();
let source = {
startColumnIndex : 0,
endColumnIndex : 4,
startRowIndex : 0,
endRowIndex : 10,
sheetId : sheetId_1
}
let destination = {
columnIndex : 5,
rowIndex : 0,
sheetId : sheetId_2
}
request.source = source;
request.destination = destination;
request.pasteType = "PASTE_NORMAL";
// Calling the Sheets API "batchUpdate" endpoint
Sheets.Spreadsheets.batchUpdate({"requests": [{"cutPaste" : request }]}, ss.getId());
参考
Advanced Google services