Google Sheets API and Chrome Apps

2019-08-19 07:08发布

问题:

I have an app that's pulling data from Google Sheets with background.js but I am unable to use that information in the popup.js itself. I am not too familiar with Chrome Apps, what can I do in this case? How do I get Chrome Identity or the API to pass the information to popup.js? I can't implement the API call in popup.js because I need it to work in the background.

So I want to modify a sheet with the Google Sheets API and then read off it.

I am basically doing this in the background.js:

chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
    var response;
    var requestURL = "https://sheets.googleapis.com/v4/spreadsheets/" + spreadsheetId + "/values/" + range;
    var objectHTTP;
    objectHTTP = new XMLHttpRequest();
    objectHTTP.onreadystatechange = function() {

    if (objectHTTP.readyState == 4 && objectHTTP.status == 200) {
            response = objectHTTP.responseText;
        }
    };

    objectHTTP.open("GET", requestURL, false);
    objectHTTP.setRequestHeader('Authorization', 'Bearer ' + token);
    objectHTTP.send();

    // This is the part where I obtain the response, but how do I use it in the main app (popup.js)? Can I somehow share the variables?
    });

回答1:

You can use the background.js to update another sheet and then use your popup.js to read from it. This way the second sheets acts as data storage.