copyTo 使用 {contentsOnly:true} 不起作用 [英] copyTo using {contentsOnly:true} not working

查看:19
本文介绍了copyTo 使用 {contentsOnly:true} 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要注意的是,我对 Google Apps 脚本编写非常陌生.感谢您提供的任何帮助.

As a caveat, I am very new to Google Apps scripting. I appreciate any assistance that can be provided.

我正在尝试将工作表的内容复制到新文档中.此代码可以正常工作:

I am trying to copy the contents of a sheet into a new document. This code works without any problem:

// Create a new Spreadsheet and copy the current sheet into it.
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
var projectname = SpreadsheetApp.getActiveSpreadsheet();
sheet = originalSpreadsheet.getActiveSheet();
sheet.copyTo(newSpreadsheet);

但是,这会复制当前工作表中的公式 - 我试图仅复制值,因为公式引用了原始文档中其他工作表上的数据.

However, this copies over the formulas from the current sheet - I am trying to copy the values only, as the formulas reference data on other sheets in the original document.

我这样做的尝试如下:

// Create a new Spreadsheet and copy the current sheet into it.
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
var projectname = SpreadsheetApp.getActiveSpreadsheet();
sheet = originalSpreadsheet.getActiveSheet();
sheet.copyTo(newSpreadsheet, {contentsOnly:true})

但是,这会产生以下错误:找不到方法(类)copyTo($Proxy914,object).

However, this generates the following error: Cannot find method (class)copyTo($Proxy914,object).

我不确定我做错了什么.任何援助将不胜感激.提前致谢!

I am unsure what I am doing wrong. Any assistance would be appreciated. Thanks in advance!

推荐答案

实际上有 2 个 copyTo,一个 适用于工作表 和其他适用于范围

There are actually 2 copyTo, one applies to sheet and the other applies to Range

根据文档(见上面的链接),第二个有可选参数,仅当第一个没有时复制值.

According to the documentation (see links above), the second one has optional argument to copy values only while the first has not.

我猜你可以做的是使用 Range.copyTo() 将整个工作表范围复制到临时工作表(在同一个电子表格中),然后将该临时工作表复制到另一个电子表格最后从源电子表格中删除临时工作表.

What I guess you could do is use the Range.copyTo() to copy the whole sheet range to a temporary sheet (in the same spreadsheet) and then copy that temporary sheet to the other spreadsheet and finally delete the temporary sheet from the source spreadsheet.

希望它足够清楚;-)

这篇关于copyTo 使用 {contentsOnly:true} 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆