2

it's known that in order to get the values from rows-columns in the Google sheets API, the format is something like this:

gapi.client.sheets.spreadsheets.values.get({
  spreadsheetId: id,
  range: "myData!A1:E" 
}).then(res => {
  console.log(res.result);
}, err => console.log(err));

Notice the "range" has to be within specific parameters, but how do I simply get the values in the max range? Is there some shorthand like "myData!Min:Max" ? I know that you can make another request to get the max row and column length:

sheets.spreadsheets.get({
    spreadsheetId: id,
    ranges: [],
    auth: something
}, (err, resp) => {
    var sheetOne = resp.data.sheets[0];
    console.log(sheetOne.properties.gridProperties); // { rowCount: 1024, columnCount: 7 } (for example)
})

and then once I have that I can theoretically automatically generate the range string with a number-to-letter switching-system to get the column letter names or something like that, and make another request to get the max columns, but first of all:

  1. It might be complicated to make a function that returns a letter-format range from just number maxes
  2. even so, I would like to get only the range that have non-empty values in it; if I have a 1000 row spreadsheet but only using the first 20 rows and columns, then I Want to automatically limit the range to the actual values.

Any way to do this with the API???

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Yaakov5777
  • 309
  • 5
  • 15
  • _a function that returns a letter-format range from just number maxes_ There are many ways to do this (Google stackoverflow for details) but an [excellent solution](https://stackoverflow.com/a/21231012/1330560) called `columnToLetter` and `letterToColumn` was presented by AdamL. – Tedinoz Mar 17 '19 at 10:07
  • Have you tried just using the R1C1 notation? Also note that trailing empty rows / columns are not returned, per documentation. – tehhowch Mar 18 '19 at 13:31
  • It’s strange that there is no answer to this yet. I have the same exact question. @tehhowch what is R1C1 notation? The question is about how a developer can get the number of rows and columns in the first place to make that notation even possible. – ariestav Jan 24 '20 at 09:35
  • @ariestav Google it, it is one of two common spreadsheet addressing styles. OP already showed how you can get the number of rows and columns. – tehhowch Jan 24 '20 at 13:28
  • @tehhowch Thanks for the tip on googling it, I hadn't though of that :D I think by R1C1 you mean "A1 Notation"? – ariestav Jan 24 '20 at 15:19
  • 1
    @ariestav No, "A1 Notation" and "R1C1 Notation" are different styles of spreadsheet addressing. The cell referred to by "F1" and the cell referred to by "R1C6" are the same cell. – tehhowch Jan 25 '20 at 04:14
  • @tehhowch thanks for clarifying! much appreciated – ariestav Jan 25 '20 at 16:00

1 Answers1

11

To return all the cells in the sheet named myData, you can use myData, rather than myData!A1:E:

gapi.client.sheets.spreadsheets.values.get({
    spreadsheetId: id,
    range: "myData" 
}).then(res => {
    console.log(res.result);
}, err => console.log(err));

With regard to your second question, as far as I know, there is no way to get only the range that has non-empty values in it using A1 notation.

However, you could iterate through the values once the api has returned them to determine which have values.

From https://developers.google.com/sheets/api/guides/concepts

A1 notation:

Some API methods require a range in A1 notation. This is a string like Sheet1!A1:B2, that refers to a group of cells in the spreadsheet, and is typically used in formulas. For example, valid ranges are:

  • Sheet1!A1:B2 refers to the first two cells in the top two rows of Sheet1.
  • Sheet1!A:A refers to all the cells in the first column of Sheet1.
  • Sheet1!1:2 refers to the all the cells in the first two rows of Sheet1.
  • Sheet1!A5:A refers to all the cells of the first column of Sheet 1, from row 5 onward.
  • A1:B2 refers to the first two cells in the top two rows of the first visible sheet.
  • Sheet1 refers to all the cells in Sheet1.
Mark Longhurst
  • 131
  • 2
  • 6
  • great explanation! How to choose the 2nd sheet in the document? – Shajeel Afzal Jul 17 '21 at 21:43
  • 1
    If you know that the sheet you want is the second sheet in a spreadsheet, and you _don't_ know the name of that sheet, I think you can get an array of the names of the sheets in a spreadsheet by passing the spreadsheetId as explained here: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets You can then choose the second element of that array to get the sheet name, and then using that sheet name, use the method outlined above to get the range. – Mark Longhurst Jul 19 '21 at 01:36