javascript - 在 Chrome 扩展中使用 Google API Spreadsheet JS 删除值

标签 javascript google-apps-script google-chrome-extension fetch google-sheets-api

我有代码可以向谷歌文档发送请求并添加值。 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/

相关文章:

javascript - 存储随机句子生成器的结果

javascript - 在循环内运行 ContactsApp.getContact(email) 会导致超时

google-apps-script - 确定执行 UrlFetchApp 调用的节点的 IP 地址

google-chrome-extension - Chrome 文本转语音 API 不起作用

javascript - 如何从 chrome 调试 API 中捕获时间线数据

javascript - 检测在我的 Chrome 扩展之外生成的 DOM 更改

javascript - 使用 Ember.js 在模板中显示模型 JSON 数据

javascript - JS - 创建并传递一个函数,该函数将在另一个作用域的上下文中进行评估

google-apps-script - 仅当符合条件时才查找编号最大的表条目

Javascript - 简单地识别浏览器连接的网络