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

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

问题描述

请注意,我是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})

但是,这会产生以下错误:无法找到方法(class)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.

希望这很清楚;-)

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

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