google-apps-script - 谷歌脚本功能 - 复制粘贴
问题描述
我正在通过 Google Script 编写一个关于单击按钮功能的脚本。我想要发生的事情是将 SHEET 1 Values 复制到 SHEET 2 AS VALUES(不复制 Google Sheets 公式),然后 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"
}
推荐阅读
- php - 卷曲返回空响应
- mongodb - 如何在 MongoDB 中查找具有特殊字符的数据
- c# - Neo4jClient 从所有关系中获取类型的集合
- javascript - 如何选择方法并将其应用于单击的任何一个元素
- node.js - Sequelize create return id: undefined
- typescript - 如何在我的 Web api 中过滤来自打字稿的帖子数据
- git - 为什么 git diff-tree 不显示整个路径
- java - 嵌入式 Kafka Spring 测试在嵌入式 Kafka 准备就绪之前执行
- c# - Minecraft 身份验证服务器返回 403 被禁止
- node.js - 这是在节点 js 中导出的一种写入方式吗