简体   繁体   中英

I cannot send a part of data from chrome extension to Spreadsheet via GAS

I want to send some data from chrome extension to Spreadsheet via GAS, but only "last" is dropped, although "Title" and "URL" is sent successfully.

Hence, could you kindly check the code?

const gasUrl = "here is my spreadsheet GAS url";

chrome.tabs.query({active: true, currentWindow: true}, tabs => {
  title = tabs[0].title;
  url = tabs[0].url;
  last = url.slice(-8); // I want to send url's last 8 letters.
  console.log(`Title: ${title}`);
  console.log(`URL: ${url}`);
  console.log(`Last: ${last}`); // I can see "last" is worked as intend here.
});

$(function() {
  $('#doit').on('click', function() {
    post2GAS(title, url, last);
  });
});

function post2GAS(title, url, last){
  const data = {
    title: title,
    url: url,
    last: last
  }

  $.ajax({
    type: 'POST',
    url: gasUrl,
    data: JSON.stringify(data)
  })
    .done(function (data) {
      console.log("success"); // I can see success on console.
    })
    .fail(function (jqXHR, textStatus, errorThrown) {
      console.log("failed");
      console.log("XMLHttpRequest : " + jqXHR.status);
      console.log("textStatus : " + textStatus);
      console.log("errorThrown : " + errorThrown);
    })
    .always((data) => {
      console.log("finished"); // I can see finished on console.
    });
}

Seems GAS cannot catch "last" param correctly, but I cannot find specific reason.

Here is GAS code.

function doPost(e) {
  var params = JSON.parse(e.postData.getDataAsString());
  var title  = params.title;
  var url    = params.url;
  var last = params.last;

  var sheet = SpreadsheetApp.openById('Sheet ID').getSheetByName("Sheet Name");
  sheet.appendRow([title, url, last]);

  var output = ContentService.createTextOutput();
  output.setMimeType(ContentService.MimeType.JSON);
  output.setContent(JSON.stringify( { sucsess: true }));
  return output;
}

Thanks,

I'm so sorry, I have not deploy new GAS. And when I deployed, it's solved.

Hence, I'm close this ticket here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM