- You want to retrieve the row number by searching a value using Sheets API.
- You want to delete the rows by searching a value using Sheets API.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Issue and workaround:
Unfortunately, in the current stage, the row number cannot be directly retrieved by searching a value with Sheets API. And also, the rows cannot be deleted by searching a value using Sheets API. I'm not sure whether these are added at the future update. So in the current workaround, how about the following workarounds?
Workaround 1:
The flow of this workaround is as follows.
- Retrieve all sheet names and sheet ID using the method of "spreadsheets.get" in Sheets API.
- Retrieve all values from the sheet in the Spreadsheet using the method of "spreadsheets.values.batchGet" Sheets API.
- Search the sheet name and row number from the retrieved values using a value.
- In this case, it is required to prepare a script.
- Delete rows using the method of "spreadsheets.batchUpdate" in Sheets API.
By this flow, your goal can be achieved.
Workaround 2:
In this workaround, Web Apps created by Google Apps Script is used as an API.
1. Create new project of Google Apps Script.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
2. Prepare script.
Please copy and paste the following script to the script editor. This script is for the Web Apps. In this script, Sheets API is used. So please enable Sheets API at Advanced Google services.
function doGet(e) {
var findText = e.parameter.value;
var spreadsheetId = e.parameter.spreadsheetId;
var deleteRows = e.parameter.deleteRows && e.parameter.deleteRows.toLowerCase() === "true";
var ss = SpreadsheetApp.openById(spreadsheetId);
var ranges = ss.createTextFinder(findText).findAll();
if (deleteRows === true) {
var requests = ranges.reverse().map(r => ({deleteDimension:{range:{dimension:"ROWS",sheetId:r.getSheet().getSheetId(),startIndex:r.getRow() - 1,endIndex:r.getRow()}}}));
Sheets.Spreadsheets.batchUpdate({requests: requests}, spreadsheetId);
}
var res = ranges.map(r => ({range: `'${r.getSheet().getSheetName()}'!${r.getA1Notation()}`, sheetId: r.getSheet().getSheetId()}));
return ContentService.createTextOutput(JSON.stringify(res));
}
- In this case, the GET method is used. When you want to run the function by giving the large data, you can use
doPost()
instead of doGet()
.
3. Deploy Web Apps.
- On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
- Select "Me" for "Execute the app as:".
- By this, the script is run as the owner.
- Here, when "Anyone" is set, the script is run as each user. In this case, it is required to share the script to each user. And the access token is required to be used. Please be careful this.
- Select "Anyone, even anonymous" for "Who has access to the app:".
- In this case, no access token is required to be request. I think that as the test case, I recommend this setting.
- Of course, you can also use the access token. At that time, please set this to "Anyone".
- Click "Deploy" button as new "Project version".
- Automatically open a dialog box of "Authorization required".
- Click "Review Permissions".
- Select own account.
- Click "Advanced" at "This app isn't verified".
- Click "Go to ### project name ###(unsafe)"
- Click "Allow" button.
- Click "OK".
- Copy the URL of Web Apps. It's like
https://script.google.com/macros/s/###/exec
.
- When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
3. Run the function using Web Apps.
This is a sample curl command for using Web Apps. Please set your Web Apps URL, spreadsheetId and the search value.
curl -GL \
-d "spreadsheetId=###" \
-d "value=sample" \
"https://script.google.com/macros/s/###/exec"
- When
spreadsheetId=###
and value=sample
are used as the query parameter at doGet(e)
. For example, you can retrieve spreadsheetId
using e.parameter.spreadsheetId
.
At above script of Web Apps, when this curl command is run, the value like [{"range":"'Sheet1'!A1","sheetId":0},{"range":"'Sheet2'!B2","sheetId":###}]
is returned. This is the result by searching a value of sample
.
- As a sample case, the value is searched from all sheets in the Spreadsheet. About this, you can modify the script for your actual situation.
If deleteRows=true
is used like below, the value is searched from all sheets in the Spreadsheet and the searched rows are deleted.
curl -GL \
-d "spreadsheetId=###" \
-d "value=sample" \
-d "deleteRows=true" \
"https://script.google.com/macros/s/###/exec"
Note:
- This sample script is used with V8.
References:
If I misunderstood your question and this was not the direction you want, I apologize.