google-apps-script - Google 工作表:将工作表复制到其他电子表格时出错
问题描述
我正在尝试使用此代码将工作表复制到其他电子表格中。但是,有错误:[例外:指定的工作表必须是电子表格的一部分]
function copy_to_A() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var destination = SpreadsheetApp.openById(Bestwise_id);
var tempsheet = source.getSheetByName('Bestwise-tocopy');
destination.insertSheet('New Sheet Name', 0, {template: tempsheet});
}
解决方案
问题和解决方法:
不幸的是,似乎当源工作表的电子表格与目标电子表格不同时,insertSheet
不能用于其他电子表格。这似乎是当前的规范。那么当你想使用时destination.insertSheet('New Sheet Name', 0, {template: tempsheet});
,下面的解决方法怎么样?此解决方法的流程如下。
- 将工作
tempsheet
表从源电子表格复制到目标电子表格。 - 使用
insertSheet('New Sheet Name', 0, {template: tempsheet})
,使用复制的纸张。 - 删除复制的工作表。
当这个流程反映到您的脚本时,它变成如下。
修改后的脚本:
function copy_to_A() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var destination = SpreadsheetApp.openById(Bestwise_id);
var tempsheet = source.getSheetByName('Bestwise-tocopy');
// 1. Copy `tempsheet` sheet from source Spreadsheet to destination Spreadsheet.
var copiedSheet = tempsheet.copyTo(destination);
// 2. Using `insertSheet('New Sheet Name', 0, {template: tempsheet})`, the copied sheet is used.
destination.insertSheet('New Sheet Name', 0, { template: copiedSheet });
// 3. Delete the copied sheet.
destination.deleteSheet(copiedSheet);
}
参考:
推荐阅读
- visual-studio - 在 c++ 中为 15 谜题找到序列 1、...、15 的随机排列
- graphql - 如何通过graphql模式从hasura graphql引擎导入关系?
- unity3d - Astar Pathfinding Project 将 AI 代理的 z 位置设置为 2D 项目中的错误值
- c - 如何在 2D 数组存储的 char 而不是 [posX] [posY] 值上执行 IF 语句
- django - 交易的django计算问题
- flutter - 错误:flutter/lib/ui/ui_dart_state.cc(199) 未处理的异常:空值检查运算符用于空值
- java - Eclipse RCP:如何使用 Workbench Site 键绑定服务注册 Jface 操作?
- python-3.x - 如何使用 PyPDF2 或任何其他包从旋转的 PDF 中提取文本而不将其从 Web 响应对象中保存?
- reactjs - reactPlayer 无法播放本地破折号(mpd)文件它返回 403 状态代码(禁止错误)
- java - JavaFX 应用程序在 IDE 中运行,带有 .bat,但双击时不运行