我试图从一个电子表格复制/传输一个电子表格到另一个电子表格。我尝试过各种方法,.copyTo似乎是最好和最有效的方法。
.copyTo运行得很好,但我很难把它发送到一个特定的页面.
这是我的代码:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetA.copyTo(destination); // I tried renaming .setName(each);
}
因此,如果我只使用sheetA.copyTo(目的),它只是创建一个副本表,就像the_souce_sheet_name的副本一样。如果我尝试重命名,使它成为一个特定的名称,我将得到错误后,运行后,该表已经存在于目标电子表格第二次。
我真正需要实现的是,源电子表格中的函数将数据从源表复制到目标电子表格中的始终相同的表中。也许.copyTo不是正确的方法吗?任何建议和代码,请帮助!
在目标电子表格中的精确工作表上接收数据的原因是,在更改时有一个触发器,该触发器执行另一个脚本来处理新传入的数据。
发布于 2019-06-06 00:09:41
var each = "Data_Incoming"
的工作表名。如果我的理解是正确的,那么这个答案呢?我想提出两个样本。因此,请为您的情况选择其中之一。
示例脚本1:
此示例脚本的流程如下所示。
each
表。each
。修改脚本:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destination.deleteSheet(destSheet);
}
sheetA.copyTo(destination).setName(each);
}
示例脚本2:
此示例脚本的流程如下所示。
each
的工作表,则将复制的表重命名为each
。each
的工作表,则源表作为Copy of ###
复制到电子表格B。each
表已被清除。each
工作表中。修改脚本:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var copiedSheet = sheetA.copyTo(destination);
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destSheet.clear();
var srcRange = copiedSheet.getDataRange();
srcRange.copyTo(destSheet.getRange(srcRange.getA1Notation()));
destination.deleteSheet(copiedSheet);
} else {
copiedSheet.setName(each);
}
}
注意:
each
表。所以请小心点。
参考文献:
如果我误解了你的问题,而这不是你想要的结果,我道歉。
添加:
下面的示例脚本用于实现上述情况。
目标电子表格的脚本:
首先,编写目标电子表格的脚本。
剧本:
function doGet() {
sample(); // This is the function that you want to run when the source values are copied.
return ContentService.createTextOutput();
}
将上述脚本复制并粘贴到目标电子表格的脚本编辑器后,请执行以下流程。
1. Copy "Current web app URL:".
- It's like `https://script.google.com/macros/s/#####/exec`.
1. Click "OK".
源电子表格的脚本:
下一步,源电子表格的脚本准备如下。在这个样本中,使用了样本2。我认为您也可以使用示例1。
剧本:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var copiedSheet = sheetA.copyTo(destination);
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destSheet.clear();
var srcRange = copiedSheet.getDataRange();
srcRange.copyTo(destSheet.getRange(srcRange.getA1Notation()));
destination.deleteSheet(copiedSheet);
} else {
copiedSheet.setName(each);
}
// Added
var url = "https://script.google.com/macros/s/###/exec"; // Please set the retrieved URL of Web Apps.
UrlFetchApp.fetch(url);
}
注意:
TransferDataOut()
时,目标电子表格的doGet()
由UrlFetchApp.fetch(url)
运行。参考文献:
https://stackoverflow.com/questions/56472894
复制相似问题