Skip to content

Latest commit

 

History

History
1163 lines (933 loc) · 45.4 KB

README.md

File metadata and controls

1163 lines (933 loc) · 45.4 KB

Google Sheets

Index


Raw requests

The following examples show the simplest requests possible depending on the action you want to perform.


Raw: Create sheet

The following is quite a simple example of adding a sheet, according to the object structure.

$this->performRequest(
    method: "POST",
    endpoint: '1234567890:batchUpdate',
    body: json_encode(
      [
        "requests" => [
          [
            "addSheet": [
              "properties" => [
                "title" => "My sheet"
              ]
            ]
          ]
        ]
      ]
    ),
);

Raw: Write cells

The request body contains an instance of ValueRange

$this->performRequest(
    method: "PUT",
    endpoint: '1234567890/values/Sheet1!A1:B2',
    query: [
        "valueInputOption" => 'USER_ENTERED',
    ],
    body: json_encode([
      "range": 'Sheet1!A1:B2',
      "values" => [
          [
              "A1", "B1", "C1"
          ],
          [
              "A2", "B2", "C2"
          ],
          [
              "A3", "B3", "C3"
          ]
      ],
      "majorDimension": "ROWS"
    ]),
);

Raw: Update cells

The following is quite a simple example of updating cells, according to the object structure.

$this->performRequest(
    method: "POST",
    endpoint: '1234567890:batchUpdate',
    body: json_encode(
      [
        "requests" => [
          [
            "updateCells": [
              "range": [
                  "sheetId": 0,
                  "startRowIndex": 0,
                  "endRowIndex": 3,
                  "startColumnIndex": 0,
                  "endColumnIndex": 3
              ],
              "rows": [
                  [
                      "values": [
                          [
                              "userEnteredValue": [
                                  "stringValue": "A1"
                              ]
                          ],
                          [
                              "userEnteredValue": [
                                  "stringValue": "B1"
                              ]
                          ],
                          [
                              "userEnteredValue": [
                                  "stringValue": "C1"
                              ]
                          ]
                      ]
                  ],
                  [
                      "values": [
                          [
                              "userEnteredValue": [
                                  "stringValue": "A2"
                              ]
                          ],
                          [
                              "userEnteredValue": [
                                  "stringValue": "B2"
                              ]
                          ],
                          [
                              "userEnteredValue": [
                                  "stringValue": "C2"
                              ]
                          ]
                      ]
                  ],
                  [
                      "values": [
                          [
                              "userEnteredValue": [
                                  "stringValue": "A3"
                              ]
                          ],
                          [
                              "userEnteredValue": [
                                  "stringValue": "B3"
                              ]
                          ],
                          [
                              "userEnteredValue": [
                                  "stringValue": "C3"
                              ]
                          ]
                      ]
                  ]
              ],
              "fields": "userEnteredValue"
            ]
          ]
        ]
      ]
    ),
);

Raw: Add chart

The following is quite a simple example of adding a chart to a sheet, according to the object structure.

$this->performRequest(
    method: "POST",
    endpoint: '1234567890:batchUpdate',
    body: json_encode(
      [
        "requests" => [
          [
            "addChart": [
              "chart": [
                  "spec": [
                      "title": "Chart title",
                      "basicChart": [
                          "chartType": "COLUMN",
                          "legendPosition": "BOTTOM_LEGEND",
                          "axis": [
                              [
                                  "position": "BOTTOM_AXIS",
                                  "title": "X axis"
                              ],
                              [
                                  "position": "LEFT_AXIS",
                                  "title": "Y axis"
                              ]
                          ],
                          "domains": [
                              [
                                  "domain": [
                                      "sourceRange": [
                                          "sources": [
                                              [
                                                  "sheetId": 0,
                                                  "startRowIndex": 0,
                                                  "endRowIndex": 1,
                                                  "startColumnIndex": 0,
                                                  "endColumnIndex": 1
                                              ]
                                          ]
                                      ]
                                  ]
                              ]
                          ],
                          "series": [
                              [
                                  "series": [
                                      "sourceRange": [
                                          "sources": [
                                              [
                                                  "sheetId": 0,
                                                  "startRowIndex": 0,
                                                  "endRowIndex": 1,
                                                  "startColumnIndex": 1,
                                                  "endColumnIndex": 2
                                              ]
                                          ]
                                      ]
                                  ],
                                  "targetAxis": "LEFT_AXIS"
                              ]
                          ]
                      ]
                  ],
                  "position": [
                      "newSheet": true
                  ],
              ]
            ]
          ]
        ]
      ]
    ),
);

Request methods

The following are non-static methods that can be called from instances (client instances) of the SheetsApi class.


Spreadsheets

  • getSpreadsheetData: Array

    Gets the spreadsheet data.

    Parameters
    • Required

      • spreadsheetId: Integer
        ID of the Spreadsheet to get data from.


Sheets

  • clearSheet(): Array

    Clears the sheet.

    Parameters
    • Required

      • spreadsheetId: String
        ID of the spreadsheet where the sheet is located.
    • Optional

      Union field (only one of the following is allowed)

      Note: If none of the following is specified, the first sheet will be cleared

      • sheetId: Integer | null
        The ID of the sheet to be cleared.
        Defaults to null.

      • sheetIndex: Integer | null
        The index of the sheet to be cleared.
        Defaults to null.

      • sheetTitle: String | null
        Title of the sheet to be cleared.
        Defaults to null.

      End union field

      • method: ClearSheetMethods
        The method to use to clear the sheet.
        Defaults to ClearSheetMethods::CLEAR_CELLS.

  • copySheet(): Array

    Copies a sheet from one spreadsheet to another (or duplicates it in the same spreadsheet).

    Parameters
    • Required

      • sourceSpreadsheetId: String
        ID of the Spreadsheet to copy the sheet from.

      • sheetId: Integer
        ID of the sheet to be copied.

    • Optional

      • destinySpreadsheetId: Integer | null
        ID of the Spreadsheet to copy the sheet to. If not specified, the sheet will be copied to the same spreadsheet.
        Defaults to null.

  • createSheet(): Array

    Creates a new sheet in the spreadsheet.

    Parameters
    • Required

      • spreadsheetId: String
        ID of the Spreadsheet to add the chart to.

      • index: Integer
        The zero-based index where the sheet should be inserted.

    • Optional

      • title: String,
        The title of the chart.
        Defaults to "New Chart".

      • sheetId: Integer | null
        A custom ID to identify the sheet. If not specified, an ID will be randomly generated.
        Defaults to null.

      • spreadsheetData: Array | null
        The spreadsheet data, which can be obtained through the getSpreadsheetData() method. Submitting this data will save a request to the API. If this parameter is not specified, the data will be fetched from the spreadsheet when needed.
        Defaults to null.


  • createSheetAtStart(): Array

    Creates a new sheet at the start of the spreadsheet.

    Parameters
    • Required

      • spreadsheetId: String
        ID of the Spreadsheet to add the chart to.
    • Optional

      • title: String,
        The title of the chart.
        Defaults to "New Chart".

      • sheetId: Integer | null
        A custom ID to identify the sheet. If not specified, an ID will be randomly generated.
        Defaults to null.


  • createSheetAtTheEnd(): Array

    Creates a new sheet at the end of the spreadsheet.

    Parameters
    • Required

      • spreadsheetId: String
        ID of the Spreadsheet to add the chart to.
    • Optional

      • title: String,
        The title of the chart.
        Defaults to "New Chart".

      • sheetId: Integer | null
        A custom ID to identify the sheet. If not specified, an ID will be randomly generated.
        Defaults to null.

      • spreadsheetData: Array | null
        The spreadsheet data, which can be obtained through the getSpreadsheetData() method. Submitting this data will save a request to the API. If this parameter is not specified, the data will be fetched from the spreadsheet when needed.
        Defaults to null.


  • deleteSheet(): Array

    Deletes a sheet from the spreadsheet.

    Parameters
    • Required

      • spreadsheetId: String
        ID of the Spreadsheet to add the chart to.

      • sheetId: Integer
        ID of the sheet to delete.

    • Optional

      • spreadsheetData: Array | null
        The spreadsheet data, which can be obtained through the getSpreadsheetData() method. Submitting this data will save a request to the API. If this parameter is not specified, the data will be fetched from the spreadsheet when needed.
        Defaults to null.


Cells

  • clearCells(): Array

    Clears a range of cells.

    Parameters
    • Required

      • spreadsheetId: String
        ID of the spreadsheet where the sheet is located.

      • sheetId: Integer
        ID of the sheet to delete.

    • Optional

      Union field (only one of the following is allowed)

      Note: If none of the following is specified, the first sheet will be cleared

      • sheetId: Integer | null
        The ID of the sheet to clear cells from.
        Defaults to null.

      • sheetIndex: Integer | null
        The index of the sheet to clear cells from.
        Defaults to null.

      • sheetTitle: String | null
        Title of the sheet to clear cells from.
        Defaults to null.

      End union field

      • startColumnIndex: String | Integer
        The column index of the first cell to clear.
        Defaults to "A".

      • startRowIndex: Integer
        The row index of the first cell to clear.
        Defaults to 1.

      • endColumnIndex: String | Integer
        The column index of the last cell to clear.
        Defaults to "ZZ".

      • endRowIndex: Integer
        The row index of the first cell to clear.
        Defaults to 1000000.

      • spreadsheetData: Array | null
        The spreadsheet data, which can be obtained through the getSpreadsheetData() method. Submitting this data will save a request to the API. If this parameter is not specified, the data will be fetched from the spreadsheet when needed.
        Defaults to null.


  • readCells(): Array

    Reads a range of cells.

    Parameters
    • Required

      • spreadsheetId: String
        ID of the spreadsheet where the sheet is located.

      • sheetId: Integer
        ID of the sheet to delete.

    • Optional

      Union field (only one of the following is allowed)

      Note: If none of the following is specified, the first sheet will be cleared

      • sheetId: Integer | null
        The ID of the sheet to read cells from.
        Defaults to null.

      • sheetIndex: Integer | null
        The index of the sheet to read cells from.
        Defaults to null.

      • sheetTitle: String | null
        Title of the sheet to read cells from.
        Defaults to null.

      End union field

      • startColumnIndex: String | Integer
        The column index of the first cell to read.
        Defaults to "A".

      • startRowIndex: Integer
        The row index of the first cell to read.
        Defaults to 1.

      • endColumnIndex: String | Integer
        The column index of the last cell to read.
        Defaults to "ZZ".

      • endRowIndex: Integer
        The row index of the first cell to read.
        Defaults to 1000000.

      • spreadsheetData: Array | null
        The spreadsheet data, which can be obtained through the getSpreadsheetData() method. Submitting this data will save a request to the API. If this parameter is not specified, the data will be fetched from the spreadsheet when needed.
        Defaults to null.


  • updateCells(): Array

    Updates a range of cells.

    Parameters
    • Required

      • spreadsheetId: String
        ID of the spreadsheet where the sheet is located.

      • sheetId: Integer
        ID of the sheet to delete.

    • Optional

      • rows: Array
        Updating rows' data and attributes.
        Defaults to [].

      • data: Array
        Updating rows' data and attributes. Simpler array format where each cell is an array of values that specifies the type and the value. Ex: [['type' => 'string', 'value' => 'Hello'], ['type' => 'number', 'value' => 123]]. If "type" is not specified, it will be considered as string. Defaults to [].

      • fields: String
        Fields to update. Fields not specified will be ignored.
        Defaults to '*'.

      Union field (only one of the following is allowed)

      Note: If none of the following is specified, the first sheet will be cleared

      • sheetId: Integer | null
        The ID of the sheet to update cells to.
        Defaults to null.

      • sheetIndex: Integer | null
        The index of the sheet to update cells to.
        Defaults to null.

      • sheetTitle: String | null
        Title of the sheet to update cells to.
        Defaults to null.

      End union field

      • startColumnIndex: String | Integer
        The column index of the first cell to be updated.
        Defaults to 1.

      • startRowIndex: Integer
        The row index of the first cell to be updated.
        Defaults to 1.

      • endColumnIndex: String | Integer
        The column index of the last cell to be updated.
        Defaults to 1000000.

      • endRowIndex: Integer
        The row index of the first cell to be updated.
        Defaults to 1000000.

      • spreadsheetData: Array | null
        The spreadsheet data, which can be obtained through the getSpreadsheetData() method. Submitting this data will save a request to the API. If this parameter is not specified, the data will be fetched from the spreadsheet when needed.
        Defaults to null.


  • writeCells(): Array

    Updates a range of cells.

    Parameters
    • Required

      • spreadsheetId: String
        ID of the spreadsheet where the sheet is located.
    • Optional

      • data: Array
        Data to be written to the cells.
        Defaults to [].

      • majorDimension: Dimension
        The major dimension that results should use.
        Defaults to Dimension::ROWS.

      Union field (only one of the following is allowed)

      Note: If none of the following is specified, the first sheet will be cleared

      • sheetId: Integer | null
        The ID of the sheet to write data to.
        Defaults to null.

      • sheetIndex: Integer | null
        The index of the sheet to write data to.
        Defaults to null.

      • sheetTitle: String | null
        Title of the sheet to write data to.
        Defaults to null.

      End union field

      • startColumnIndex: String | Integer
        The column index of the first cell to be written into.
        Defaults to "A".

      • startRowIndex: Integer
        The row index of the first cell to be written into.
        Defaults to 1.

      • endColumnIndex: String | Integer
        The column index of the last cell to be written into.
        Defaults to "ZZ".

      • endRowIndex: Integer
        The row index of the first cell to be written into.
        Defaults to 1000000.

      • spreadsheetData: Array | null
        The spreadsheet data, which can be obtained through the getSpreadsheetData() method. Submitting this data will save a request to the API. If this parameter is not specified, the data will be fetched from the spreadsheet when needed.
        Defaults to null.

      • valueInputOption: ValueInputOption
        How the input data should be interpreted.
        Defaults to ValueInputOption::USER_ENTERED.

      • responseValueRenderOption: ValueRenderOption
        How values should be represented in the output.
        Defaults to ValueRenderOption::FORMATTED_VALUE.

      • responseDateTimeRenderOption: DateTimeRenderOption
        How dates, times, and durations should be represented in the output.
        Defaults to DateTimeRenderOption::SERIAL_NUMBER.



Charts

  • addChart(): Array

    Adds a chart to a spreadsheet.

    Parameters
    • Required

      • spreadsheetId: Integer
        ID of the Spreadsheet to add the chart to

      • chartId: Integer
        Custom ID to identify the chart

      • chartData: Array
        The chart's data in array format

    • Optional

      • dataSourceId: String | null
        The ID of the data source to use for the chart. If not specified, the chart will use the first data source in the spreadsheet.
        Defaults to null.

      • filterSpecs: Array | null
        The filters to apply to the chart.
        Defaults to null.

      • sortSpecs: Array | null
        If specified, the chart will be sorted by the specified data source.
        Defaults to null.

      • title: String,
        The title of the chart.
        Defaults to "New Chart".

      • subtitle: String,
        The subtitle of the chart.
        Defaults to "".

      • fontName: String,
        The name of the font to use by default for all chart text (e.g. title, axis labels, legend).
        Defaults to "Roboto".

      Union field (only one of the following is allowed)

      Note: If none of the following is specified, the first sheet will be cleared

      • sheetId: Integer | null
        The ID of the sheet to add the chart to.
        Defaults to null.

      • sheetIndex: Integer | null
        The index of the sheet to add the chart to.
        Defaults to null.

      • sheetTitle: String | null
        Title of the sheet to add the chart to.
        Defaults to null.

      End union field

      • spreadsheetData: Array | null
        The spreadsheet data, which can be obtained through the getSpreadsheetData() method. Submitting this data will save a request to the API. If this parameter is not specified, the data will be fetched from the spreadsheet when needed.
        Defaults to null.

      • chartType: ChartTypes
        The type of the chart to be added.
        Defaults to ChartTypes::BASIC.



Objects


Sheets


addSheet


Cells


updateCells


Charts





Others


  • stringValue: String
  • numberValue: Float
  • boolValue: Boolean
  • formulaValue: String
  • errorValue: ErrorValue


  • foregroundColorStyle: ColorStyle
  • fontFamily: String
  • fontSize: Integer
  • bold: Boolean
  • italic: Boolean
  • strikethrough: Boolean
  • underline: Boolean
  • link: Link
    • uri: String

  • rgbColor: Color
    • red: Float
    • green: Float
    • blue: Float
    • alpha: Float
  • themeColor: enum: ThemeColorType

  • sheetId: Integer
  • startRowIndex: Integer
  • endRowIndex: Integer
  • startColumnIndex: Integer
  • endColumnIndex: Integer

Enums