1

I have a query to a google sheet that is working as I expect. I'm using Postman to do my testing because in my app — which is a desktop app, I cannot use any of the SDKs that Google provides. This is not a web app, so I cannot use even Javascript source that is hosted on Google's CDN. In any case, I'm querying a spreadsheet that has many rows, with a particular column that I'm querying for:

I'm using the following endpoint, after having retrieved an authentication token via OAuth2 methodology:

https://docs.google.com/a/google.com/spreadsheets/d/{{sheet_key}}/gviz/tq?sheet={{tab_name}}&tq={{sheet_query}}

The {{sheet_query}} variable in Postman, which handles the URL encoding properly is set to the following:

select * where C = 'ready'

The response for my particular worksheet is what I would expect and the data is correct. There are three rows in the sheet where column C is set to 'ready' which is great.

Now, the response to this GET request does not appear to send back the ordinal row index for the retrieved rows, it just send the rows' data. The problem is that I want to write back to these rows via the Google Sheets API v4 which now uses the A1 notation as a method to target which cell range to update.

The problem is that in order to write back to these rows retrieved via my Visualization API request, I would need the rows' ordinal indices for the requests to the Google Sheets API. Is there any way to get each row's ordinal index from the Sheet in the request to the Visualization API call?

I found this Stack Overflow thread, but I'm not exactly sure if this can work in my case as I'm essentially building the requests manually and without an SDK. I'm also not using Google Apps Script, I'm using the Visualization API. The only workaround I can think of is to force the end-user to ensure the worksheet to be queried has a column whose cells have the =ROW() formula, but I would rather not make that a requirement on the user's part.

Any insights or guidance is appreciated.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
ariestav
  • 2,799
  • 4
  • 28
  • 56
  • 1
    Can I ask you about your question? Unfortunately, the Request Format of Google Visualization API query language has no value for setting the data range. By this, unfortunately, I think that the formula of sample thread cannot be used. So as a workaround, how about using Web Apps of Google Apps Script? In this case, the row number can be retrieved by HTML request to Web Apps. Because Web Apps is used as an API. But in your question, I saw `I'm also not using Google Apps Script`. So if you cannot use Google Apps Script for your situation, I apologize. – Tanaike Feb 01 '20 at 23:49
  • @Tanaike thank you for your comment, I appreciate it. I was not aware of Web Apps as an API. Can you point me to the documentation? How would I use it to get the row index for the returned rows in the Visualization API? – ariestav Feb 02 '20 at 00:03
  • Thank you for replying. In the workaround for using Web Apps, the row number is directly retrieved by searching with the value in the Spreadsheet. So in this case, Visualization API is not used. When it accesses to Web Apps, the row number can be directly retrieved with the Web Apps. Is this workaround the same with the direction you want? If it's not, I apologize. The reference is [Ref](https://developers.google.com/apps-script/guides/web). – Tanaike Feb 02 '20 at 00:08
  • I'm not sure if it's the direction I want the app to go in. The app I build is for end-users who are making their own Sheets, so I do not want them to have to do anything other than setup the sheet in the way they want. Can I use the Web Apps API to build my app that all end users can use? – ariestav Feb 02 '20 at 00:18
  • Thank you for replying. From your replying, for example, in your application, users use Sheets API using each access token? If it can do, I think that the method for searching a value and retrieving the row number from the values retrieved by Sheets API might be suitable. How about this? – Tanaike Feb 02 '20 at 00:41
  • My understanding is that it is not possible to query using Sheets API. I’m not sure I understand your last comment. Do you man the Web App API? – ariestav Feb 02 '20 at 01:59
  • Thank you for replying. I apologize for my poor English skill. About `it is not possible to query using Sheets API.`, it's yes. So I proposed the method for searching a value and retrieving the row number using the values retrieved by Sheets API. In this case, the script is required to be prepare, although I'm not sure about the language you use. – Tanaike Feb 02 '20 at 02:06
  • And from your replying, I thought that the Web Apps might not be suitable for your actual situation. Because I thought that the Spreadsheet of each user might not be publicly shared, and also, when the Web Apps is used, the Web Apps is required to be prepared by each user. – Tanaike Feb 02 '20 at 02:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207041/discussion-between-ariestav-and-tanaike). – ariestav Feb 02 '20 at 02:14
  • Can't you use Sheets API to retrieve both the data and the row indexes? [GridData](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/sheets#GridData) contains the field `rowData`, whose value is an array with the values for each row. You can use the array index to know the row index. – Iamblichus Feb 03 '20 at 11:07
  • @iamblichus that would work great if the query return a range that was continuous, but not if GridData returns a set of discontiguous rows based on some query that is used via the Visualization API. You cannot depend on the array's index being the actual row number. – ariestav Feb 03 '20 at 14:20
  • @ariestav what I mean is returning all rows in the sheet and, when you have that, check which lines have `ready` in column C. You can keep track of the row index this way. Visualization API would not be used in this case. – Iamblichus Feb 03 '20 at 15:41
  • Some of the sheets our end users build have 20K to 40K rows. I am not going to receive a payload like that and then overwhelm the client machine’s processor just to get a row number. – ariestav Feb 03 '20 at 15:42
  • @Tanaike you mentioned that Google Apps Script can query sheets for rows whose cells have specific values AND get the row number. How is this possible? – ariestav Feb 05 '20 at 21:02
  • Thank you for replying. When Google Apps Script is used, the script can retrieve the row number by searching a value from the Spreadsheet. You need a sample script for this? or You need the document for this? – Tanaike Feb 05 '20 at 23:25
  • @Tanaike a sample script would be great. I looked through docs but could not determine which methods to use. Many thanks! – ariestav Feb 05 '20 at 23:41
  • Thank you for replying. As a sample script, I proposed it as an answer. Could you please confirm it? If that was not the result you want, I apologize. – Tanaike Feb 05 '20 at 23:54

1 Answers1

2
  • You want to retrieve the row numbers by searching a value from the sheet in Google Spreadsheet.
  • You want to achieve this using Google Apps Script.

I could understand like above. If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Flow:

  1. Retrieve the sheet object.
  2. Search values in a sheet using a search value.
    • In this case, I used TextFinder.
  3. Retrieve the row numbers from the searched result.

Sample script:

Please copy and paste the following sample script to the script editor, and set the variables of searchValue, spreadsheetId, sheetName. Then, as a test run, please run the function of testRun(). By this, the row numbers are returned as an array.

function getRowNumberBySearch(searchValue, spreadsheetId, sheetName) {
  var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
  var t = sheet.createTextFinder(searchValue).findAll();
  return t.map(function(e) {return e.getRow()});
}

// Please run this function.
function testRun() {
  var searchValue = "sample";
  var spreadsheetId = "###";
  var sheetName = "Sheet1";

  var res = getRowNumberBySearch(searchValue, spreadsheetId, sheetName);
  Logger.log(res)
}

References:

Added:

Additional question 1:

is there a way to narrow the .findAll() method to a specific range within the sheet so that it focuses on a single column?

Sample script:
function getRowNumberBySearch(searchValue, spreadsheetId, sheetName, range) {
  var range = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName).getRange(range);
  var t = range.createTextFinder(searchValue).findAll();
  return t.map(function(e) {return e.getRow()});
}
  • In this case, range is a1Notation.

Additional question 2:

is it possible to return all of the values of the row and not just their index?

Sample script:
function getRowNumberBySearch(searchValue, spreadsheetId, sheetName, range) {
  var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
  var t = sheet.getRange(range).createTextFinder(searchValue).findAll();
  return t.map(function(e) {return sheet.getRange(e.getRow(), 1, 1, sheet.getLastColumn()).getValues()[0]});
}
  • In this case, range is a1Notation.
  • I think that in this case, the endpoint of your question can be also used.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • this work in that it does in fact return the row numbers based on the TextFinder `.findAll()` method. Thank you. However, is there a way to narrow the `.findAll()` method to a specific range within the sheet so that it focuses on a single column? And, is it possible to return all of the values of the row and not just their index? – ariestav Feb 06 '20 at 09:44
  • 1
    @ariestav Thank you for replying. I apologize for my incomplete answer. About your additional 2 questions, I added them to my answer. Could you please confirm it? If I misunderstood your questions, I apologize. – Tanaike Feb 06 '20 at 11:56
  • I very much appreciate your help and your time in doing so. Thank you. – ariestav Feb 06 '20 at 20:49