Reputation: 8317
I need to find a row in my spreadsheet that contains some data and delete it. all I know is the id inside a cell on the row.
How can I perform a search for the location of the cell with google REST API
Like for example:
GET https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values:find?value=123456
response:
sheet1! B2:B2
Also: I also want to support cut&paste from one sheet to another. so put metadata on the row can't work because it lost when copying cells from the row to another row. ( there is no REST API to cut&paste dimension (row||column) from one sheet to another.
If there is a way to copy&paste dimension between sheet I simple can use
POST https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values:batchGetByDataFilter
{
"dataFilters": [
{
object (DataFilter)
}
],
"majorDimension": enum (Dimension),
"valueRenderOption": enum (ValueRenderOption),
"dateTimeRenderOption": enum (DateTimeRenderOption)
}
to find the row and delete it.
Upvotes: 1
Views: 2389
Reputation: 201378
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
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?
The flow of this workaround is as follows.
By this flow, your goal can be achieved.
In this workaround, Web Apps created by Google Apps Script is used as an API.
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.
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));
}
doPost()
instead of doGet()
.https://script.google.com/macros/s/###/exec
.
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"
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
.
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"
If I misunderstood your question and this was not the direction you want, I apologize.
Upvotes: 2