Carlos27
Carlos27

Reputation: 71

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)
}

Upvotes: 7

Views: 1479

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

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
          }
        }
      }
    }
  ]
}

Upvotes: 1

Related Questions