Reputation: 13
Is there a find and replace method in Google Spreadsheet api? there somewhere I found find and replace request but unable to determine what URL should I use for that. Google's documentation isn't much clear about that.
Upvotes: 1
Views: 1154
Reputation: 201378
About find and replace request
, I guessed that you might be thinking of FindReplaceRequest, you can use this with "Method: spreadsheets.batchUpdate" of Sheets API.
The sample request is as follows.
The content type of the request header is application/json
.
POST https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}:batchUpdate
{
"requests": [
{
"findReplace": {
"sheetId": 0,
"find": "sample",
"replacement": "replaced",
"matchEntireCell": true
}
}
]
}
sample
is searched and it is replaced with a value of replaced
in the specification sheet in Google Spreadsheet."sheetId": 0
to "allSheets": true
.The sample curl command is as follows. I thought that the sample curl command might help to think of the script you are using.
$ curl --request POST \
'https://sheets.googleapis.com/v4/spreadsheets/[SPREADSHEETID]:batchUpdate' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"requests":[{"findReplace":{"allSheets":false,"sheetId":0,"find":"","replacement":"","matchEntireCell":false}}]}' \
--compressed
When I saw your tag, I noticed javascript
. So, I added Javascript as a sample script. In this sample, the sheet ID of 0
is used. Please modify this for your actual situation.
const spreadsheetId = "###"; // Please set your Spreadsheet ID.
gapi.client.sheets.spreadsheets.batchUpdate({
spreadsheetId,
resource: {
requests: [
{
findReplace: {
sheetId: 0,
find: "sample",
replacement: "replaced",
matchEntireCell: true
}
}
]
}
}).then(({result}) => {
console.log(result)
}, function(reason) {
console.error(reason.result.error.message);
});
// Fetch first 10 files
. But, before you use this, please modify the scope from const SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly';
to const SCOPES = 'https://www.googleapis.com/auth/spreadsheets';
.javscript
tag in your tag. If you want to achieve your goal using Google Apps Script. I think that TextFinder of Spreadsheet service (SpreadsheetApp) instead of Sheets API might be suitable.Upvotes: 3