简体   繁体   English

谷歌表格如何编写脚本:将标签复制到新文件?

[英]Google sheets how to script: copy tab to new file?

I'm new to using Google sheets, and got stuck trying to reproduce one of the simplest of Excel macro's in Google sheets.我是使用 Google 表格的新手,并且试图在 Google 表格中重现最简单的 Excel 宏之一。

What i'm trying to do:我正在尝试做的事情:

In source file在源文件中

  • jump to source tab select this tab (containing calculations) & copy (works)跳转到源选项卡 select 此选项卡(包含计算)和复制(工作)
  • jump to temp tab paste only values & format (works)跳转到临时选项卡仅粘贴值和格式(有效)
  • copy out temp tab to new file (does not work)将临时选项卡复制到新文件(不起作用)

This would be my excel code for reference这将是我的 excel 代码供参考

Sub EXPORT()
' ---------------------------------------------------------------
' GRAB SOURCE TAB & COPY
' ---------------------------------------------------------------
    Sheets("SOURCE").Select
    Cells.Select
    Selection.Copy
' ---------------------------------------------------------------
' SELECT TEMP TAB & PASTE VALUES & FORMAT (WITHOUT CALCULATIONS)
' ---------------------------------------------------------------
    Sheets("TEMP").Select
'
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
'
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
'
    Range("A1").Select
' ---------------------------------------------------------------
' COPY TEMP TAB OUT TO NEW FILE
' ---------------------------------------------------------------
    Range("A1").Select
    Sheets("TEMP").Select
    Application.CutCopyMode = False
    Sheets("TEMP").Copy

End Sub

There are a few more things I'd like to do (such as determine new file name etc. based on a cel) etc. but after 2 days of searching I just don't understand why i'm stuck on something this simple.我还想做一些其他事情(例如根据 cel 确定新文件名等)等,但经过 2 天的搜索,我只是不明白为什么我会坚持这么简单的事情。

Manually it would simply be手动它只会是

  • Right click tab右键单击选项卡
  • copy to -> new spreadsheet复制到 -> 新电子表格

I've tried more or less everything from simply recording a Macro (which doesn't seem to record the new spreadsheet action) to implementing parts of code from questions that were far more complex than mine (and ended up doing something wrong getting errors), so I truly am sorry if this is a simple question but i'm so stuck:(我已经或多或少尝试了一切,从简单地记录宏(似乎没有记录新的电子表格操作)到从比我复杂得多的问题中实现部分代码(最终做错了错误) ,所以如果这是一个简单的问题,我真的很抱歉,但我很困惑:(

thanks a lot for reading非常感谢阅读

Tim蒂姆

If your goal is to develop a macro that copies just one particular sheet to a new spreadsheet you can use Apps Script .如果您的目标是开发一个仅将一个特定工作表复制到新电子表格的宏,您可以使用Apps 脚本 Then you only have to import the Apps Script function and run it like a macro.然后你只需要导入 Apps Script function 并像宏一样运行它。

It is possible to create a new Sheet with SpreadsheetApp.create() and then populate it with Range.copyTo() .可以使用SpreadsheetApp.create()创建一个新工作表,然后使用Range.copyTo()填充它。 You can make sure that only the active sheet gets copied by using SpreadsheetApp.getActiveSheet() .您可以使用SpreadsheetApp.getActiveSheet()确保仅复制活动工作表。

function myFunction() {
  var newSpreadsheet = SpreadsheetApp.create("My new Sheet")
  SpreadsheetApp.getActiveSheet().copyTo(newSpreadsheet);
}

I will assume that you know how to import macros, but leave a comment below if you need further help.我假设您知道如何导入宏,但如果您需要进一步的帮助,请在下方留言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM