我有代码可以向谷歌文档发送请求并添加值。 put 请求运行良好,但我想删除我发送的这个值。我怎样才能做到这一点?这是我的代码: 背景-js:
chrome.identity.getAuthToken({ 'interactive': true }, getToken);
function getToken(token) {
console.log('this is the token: ', token);
var params = {
"range":"Sheet1!A1:B1",
"majorDimension": "ROWS",
"values": [
["Hello","world"]
],
}
let init = {
method: 'PUT',
async: true,
body: JSON.stringify(params),
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json'
},
'contentType': 'json',
};
fetch(
"https://sheets.googleapis.com/v4/spreadsheets/1efS6aMlPFqHJJdG8tQw-BNlv9WbA21jQlufsgtMsUmw/values/Sheet1!A1:B1?valueInputOption=USER_ENTERED",
init)
.then((response) => console.log(response))
let request = {
method: 'GET',
async: true,
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json'
},
'contentType': 'json',
};
fetch(
"https://sheets.googleapis.com/v4/spreadsheets/1efS6aMlPFqHJJdG8tQw-BNlv9WbA21jQlufsgtMsUmw/values/Sheet1!A1:B1",
request)
.then((response) => response.json())
.then(function(data) {
console.log(data)
});
}
最佳答案
您可以使用空字符串["",""]
而不是["Hello","world"]来清除范围。或者,使用 Spreadsheets.values.clearPOST
:
fetch(
"https://sheets.googleapis.com/v4/spreadsheets/[SPREADSHEET_ID]/values/Sheet1!A1:B1:clear",
Object.assign({},init,{method:'POST',body:""}))
.then((response) => console.log(response))
如果您想清除值以及格式、注释等,请使用 updateCellsRequest POST
关于javascript - 在 Chrome 扩展中使用 Google API Spreadsheet JS 删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63490252/