Google Apps脚本 - copyTo使用{contentsOnly:true}不起作用 [英] Google Apps Script - copyTo using {contentsOnly:true} not working
问题描述
我试图将表单的内容复制到新文档中。这段代码没有任何问题:
//创建一个新的Spreadsheet并将当前工作表复制到其中。
var newSpreadsheet = SpreadsheetApp.create(Spreadsheet to export);
var projectname = SpreadsheetApp.getActiveSpreadsheet();
sheet = originalSpreadsheet.getActiveSheet();
sheet.copyTo(newSpreadsheet);
但是,它会复制当前表单中的公式 - 我试图仅复制这些值,因为这些公式引用了原始文档中其他工作表的数据。
我试图做到这一点如下:
//创建一个新的电子表格并将当前表格复制到其中。
var newSpreadsheet = SpreadsheetApp.create(Spreadsheet to export);
var projectname = SpreadsheetApp.getActiveSpreadsheet();
sheet = originalSpreadsheet.getActiveSheet();
sheet.copyTo(newSpreadsheet,{contentsOnly:true})
然而,这会生成以下错误:无法找到方法(类)copyTo($ Proxy914,object)。
我不确定我做错了什么。任何援助将不胜感激。提前致谢!
实际上有2 copyTo
,一个< href =https://developers.google.com/apps-script/class_sheet?hl=fr-FR#copyTo =noreferrer>适用于工作表,另一个适用于范围
根据文档(请参阅上面的链接),第二个可选参数仅在第一个没有时复制值。
我猜你可以做的是使用 Range.copyTo()
将整个工作表范围复制到临时工作表(在同一电子表格中),然后将该临时工作表复制到另一个电子表格中,最后删除临时工来自源电子表格。
希望它足够清楚; - )
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.
My attempt to do this is as follows:
// 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})
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!
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.
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.
Hoping it is clear enough ;-)
这篇关于Google Apps脚本 - copyTo使用{contentsOnly:true}不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!