I managed to write an R function using google sheets api's 'setBasicFilter' that turns on the basic filter on the cell range specified by the user. However, when the user tries to 'Filter by values', no values appear to be selected.
The function seems to work fine in the sense that the green filter arrow (or button at the right side of the cells you are using to filter) turn on in the sheets (tabs) and cell range that I want. What I do not understand is why when I click on it, it appears as if there were no values in the column to filter by. It is completely blank but there is data on the column!
I thought perhaps it was setting a filter on top of a filter and tried using 'clearBasicFilter' first before sending 'setBasicFilter' and it did not work.
I tried writing data to the sheet and then turning on the filter and, vice versa, turning on the filter and then writing the data. Still appears as if there were no values to filter by (although the data is there).
In essence, what I am trying to achieve is, when writing data to a sheet:
- Remove any basic filter present.
- Write the data.
- Turn the filter buttons back on (within the range specified by the user of the function).
Here is what my call looks like:
# for each row in the param table create a json body
for(row in nrow(param)){
body <- paste0('{"requests": [
{
"setBasicFilter": {
"filter": {
"range":{"sheetId": ', param$sheetId,
', "startRowIndex": ', param$startRow,
', "endRowIndex": ', param$endRow,
', "startColumnIndex": ', param$startCol,
', "endColumnIndex": ', param$endCol,
'}}}}]}')
}
Where the param table looks like this (and is created based on the users' inputs to the function:
|sheetName |startRow |endRow |startCol |endCol |sheetId |
|:---------|:--------|:------|:--------|:------|:-----------|
|Sheet1 |1 |0 |0 |11 |someId |
|Sheet2 |0 |0 |0 |11 |someOtherId |
Multiple json bodies are created and then I lapply the call using google's batchUpdate method.
I do not get any errors and the filter turns on where I need it to be. I would like to be able to:
- Go to the sheet I just turned the filter on
- Click on the green filter arrow (which will already be in the cell range I specified)
- And see the list of all possible values I can filter that column by
I am sorry for such a long description. I was trying to be as specific as possible.