【问题标题】:External API call in Google Spreedsheet is possible?Google 电子表格中的外部 API 调用是否可行?
【发布时间】:2014-12-04 07:47:04
【问题描述】:

我创建了一个包含五列的 Google 电子表格;

一旦用户填写了前三列中的值,它必须调用第 3 方 API 并在第四列和第五列中填写值(响应)。

是否可以在 Google 电子表格中为 API 调用编写代码? 是否可以在 Google 电子表格中调用外部 API 并获取响应?

【问题讨论】:

标签: google-sheets spreadsheet google-spreadsheet-api


【解决方案1】:

有一种方法可以进行 API 调用并将结果输入电子表格 - 我知道的唯一方法是创建/打开目标电子表格,转到工具,然后是脚本编辑器,并将其用作绑定脚本:

function Maestro() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet (bound to this script)
var sheet = ss.getSheetByName('mae'); //The name of the sheet tab where you are sending the info

var apiCall = 'getUpcomingConference';
var apiKey = '_____key here______';
var apiToken = '______security token______';
var url = 'http://myaccount.maestroconference.com/_access/' + apiCall +"?customer=" + apiKey + "&key=" + apiToken; //api endpoint as a string 

var response = UrlFetchApp.fetch(url); // get api endpoint
var json = response.getContentText(); // get the response content as text
var mae = JSON.parse(json); //parse text into json

Logger.log(mae); //log data to logger

var stats=[]; //create empty array to hold data points

var date = new Date(); //create new date for timestamp

//The number in brackets refers to which instance we are looking at - soonest upcoming call is [0], next after that is [1], etc.
stats.push(date); //timestamp
stats.push(mae.value.conference[0].name);
stats.push(mae.value.conference[0].scheduledStartTime);
stats.push(mae.value.conference[0].UID);

//append the stats array to the active sheet 
sheet.appendRow(stats);
}

它需要一点界面工作,但功能!它从 API 调用中获取信息并将其放入电子表格中。

【讨论】:

    【解决方案2】:

    我最近遇到了读取工作表行并在请求中发送数据并记录响应的相同要求。我想我会在谷歌搜索后分享我的工作......

    function testing_this() {
        var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
        for (row in data) {
            Logger.log(data[row]);   
    
            var row = data[row]
            var options = {
                'method': 'post',
                'payload': { email:row[1]}
            };
            // sending to API. for example: 
            UrlFetchApp.fetch('https://your-rest-api-url/v1/customers/', options);
        }
    }
    

    如果你想获取工作表中的数据,你应该使用函数:

    var response = UrlFetchApp.getRequest("http://your-api-url/");
        for(data in response) {
            var respData = response[data];
            // do whatever u want to do with this data...
        }
    

    希望对遇到上述类似需求的大家有用。

    如果你想 fork/pull,我已经在 github 上发布了这个脚本......

    https://github.com/joshiparthin/gsheetScriptExperiments/blob/master/readAndSendToApi.js

    干杯,

    部分

    【讨论】:

      【解决方案3】:

      有一些 Google 表格插件可以帮助您使用 API,而无需编写自定义代码。

      其中之一是Powersheets,它提供了特殊公式来调用工作表中的 API。

      Powersheets 的API 公式接受 API GET 端点的 url 作为第一个参数。

      = API( "https://api.spacexdata.com/v4/company" )
      

      This 是最终结果的样子。

      免责声明:我是Powersheets的作者。

      【讨论】:

      • 感谢插件。只是一个查询,当我重新加载工作表时,它看不到调用 api 来用更新的数据更新单元格。有什么我想念的吗。单元格公式如下所示=JSONPATH(API("some_api_endpoint"), "price")
      • 您可以通过单击附加菜单中的“刷新”来强制重新加载数据 (example)。希望对您有所帮助。
      • 当然。谢谢。
      • 要求访问查看/编辑/下载我帐户中的所有工作表,永远不会提供。 @Powersheets
      【解决方案4】:

      我只能回应建议使用 Google 表格的 powersheets.app 扩展的答案。

      我有一长串要从一种语言翻译成另一种语言的文本段落。诸如“在单元格 A2 中是原始文本”、“在单元格 B2 中我想要一个自动翻译”之类的东西,一千次。

      Deepl.com 免费提供 API - 最多达到一定数量 - 使用 powersheets 扩展从 Google 表格调用它们非常简单:

      =JSONPATH(API(CONCATENATE("https://api-free.deepl.com/v2/translate?text=";ENCODEURL(A2);"&target_lang=DE&auth_key=xxxMyDeeplKEYxxx"));"translations.0.text”)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-07
        • 2012-10-06
        • 2019-04-18
        • 1970-01-01
        • 1970-01-01
        • 2015-09-10
        相关资源
        最近更新 更多