Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create new Google Sheets in Swift 4 using Sheets API v4

I can't figure out how to create a new Google sheets in Swift 4 using the Sheets API. I would also like the use the same Google sheets later on in my code and so I would need it to return a value for the spreadsheet ID. This is what I have so far.

func createNewSheet() {

    let url = NSURL(string: "https://sheets.googleapis.com/v4/spreadsheets")

    let task = URLSession.shared.dataTask(with: url! as URL) {
        (data, response, error) in
        print(NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!)
    }
    task.resume()
    print (task)
}
like image 280
Carlos27 Avatar asked May 02 '18 23:05

Carlos27


People also ask

How do I create a new sheet in Appscript?

var ss = SpreadsheetApp. getActiveSpreadsheet(); ss. insertSheet('My New Sheet'); Where 'My New Sheet' is the name you want it to be.


1 Answers

There's an Add a Sheet guide from the docs:

The following spreadsheets.batchUpdate request adds a sheet to a spreadsheet, while also setting the title, size, and tab color. This request returns an AddSheetResponse, consisting of an object with the created sheet's properties (such as its sheetId).

The request protocol is shown below. The Updating Spreadsheets guide shows how to implement a batch update in different languages using the Google API client libraries.

POST https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate
{
  "requests": [
    {
      "addSheet": {
        "properties": {
          "title": "Deposits",
          "gridProperties": {
            "rowCount": 20,
            "columnCount": 12
          },
          "tabColor": {
            "red": 1.0,
            "green": 0.3,
            "blue": 0.4
          }
        }
      }
    }
  ]
}
like image 119
noogui Avatar answered Oct 07 '22 19:10

noogui