简体   繁体   中英

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.

What i'm trying to do:

In source file

  • jump to source tab select this tab (containing calculations) & copy (works)
  • 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

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.

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 . Then you only have to import the Apps Script function and run it like a macro.

It is possible to create a new Sheet with SpreadsheetApp.create() and then populate it with Range.copyTo() . You can make sure that only the active sheet gets copied by using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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