Remove value using Google API Spreadsheet JS in Chrome Extension

Viewed 56

I have code which sends request to google docs and add a value. The put request is working well but I want to delete this value which I send . How can I do this? Here is my code: background-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)
          });
}
1 Answers

You can use empty strings ["",""] instead of ["Hello","world"] to clear out the range. Alternatively,POST using Spreadsheets.values.clear:

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))

If you want to clear values as well as formats, notes etc, POST with updateCellsRequest

Related