【问题标题】:Google Sheets API and Chrome AppsGoogle 表格 API 和 Chrome 应用程序
【发布时间】:2018-03-23 22:16:20
【问题描述】:

我有一个应用程序使用 background.js 从 Google 表格中提取数据,但我无法在 popup.js 本身中使用该信息。我对 Chrome 应用程序不太熟悉,在这种情况下我该怎么办?如何获取 Chrome 身份或 API 以将信息传递给 popup.js?我无法在 popup.js 中实现 API 调用,因为我需要它在后台工作。

所以我想用 Google Sheets API 修改一张表格,然后读出它。

我基本上是在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?
    });

【问题讨论】:

  • 您应该使用Message Passing,扩展程序及其内容脚本之间的通信通过使用消息传递进行。您还可以查看这两个相关的 SO 帖子 12,看看这是否适合您想要实现的目标。

标签: javascript google-chrome-extension google-chrome-app google-sheets-api


【解决方案1】:

您可以使用 background.js 更新另一个工作表,然后使用 popup.js 从中读取。这样第二张表就可以作为数据存储。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 2014-07-29
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    相关资源
    最近更新 更多