SQL Server guide

Execute sheet actions (delete rows / columns)


Executes a batch update request on a sheet to delete specific row ranges. This example calls the batch_update_request endpoint with two deleteDimension operations to remove rows 10–20 and 50–60. You can adapt the JSON body for other actions such as formatting, copy, paste, or merge.

Use tab ID 0 for the first tab, or the gid from the sheet URL for other tabs; set the spreadsheet ID to the sheet you want to update.

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

SELECT * FROM batch_update_request
WITH(
  Body='{
  "requests": [
    {
      "deleteDimension": {
        "range": {
          "sheetId": 0,
          "dimension": "ROWS",
          "startIndex": 10,
          "endIndex": 20
        }
      }
    },
    {
      "deleteDimension": {
        "range": {
          "sheetId": 0,
          "dimension": "ROWS",
          "startIndex": 50,
          "endIndex": 60
        }
      }
    }
  ]
}',
  TabId='0',  -- 0 = first tab
  SpreadSheetId='1az2H8ZYk7BvjddVTqPR-LfDjX9IRpIpjCDpFPe9EzkU'
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_GOOGLE_SHEETS_IN_GATEWAY], 'SELECT * FROM batch_update_request
WITH(
  Body=''{
  "requests": [
    {
      "deleteDimension": {
        "range": {
          "sheetId": 0,
          "dimension": "ROWS",
          "startIndex": 10,
          "endIndex": 20
        }
      }
    },
    {
      "deleteDimension": {
        "range": {
          "sheetId": 0,
          "dimension": "ROWS",
          "startIndex": 50,
          "endIndex": 60
        }
      }
    }
  ]
}'',
  TabId=''0'',  -- 0 = first tab
  SpreadSheetId=''1az2H8ZYk7BvjddVTqPR-LfDjX9IRpIpjCDpFPe9EzkU''
)')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_GOOGLE_SHEETS_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM batch_update_request
WITH(
  Body=''{
  "requests": [
    {
      "deleteDimension": {
        "range": {
          "sheetId": 0,
          "dimension": "ROWS",
          "startIndex": 10,
          "endIndex": 20
        }
      }
    },
    {
      "deleteDimension": {
        "range": {
          "sheetId": 0,
          "dimension": "ROWS",
          "startIndex": 50,
          "endIndex": 60
        }
      }
    }
  ]
}'',
  TabId=''0'',  -- 0 = first tab
  SpreadSheetId=''1az2H8ZYk7BvjddVTqPR-LfDjX9IRpIpjCDpFPe9EzkU''
)'
EXEC (@MyQuery) AT [LS_TO_GOOGLE_SHEETS_IN_GATEWAY]