【问题标题】:Refresh BigQuery data in Sheets via an API call通过 API 调用刷新表格中的 BigQuery 数据
【发布时间】:2020-04-16 14:35:07
【问题描述】:

如果 Google 表格的数据通过 BigQuery 的 Sheets 数据连接器 来自 BigQuery(如 here 所述),是否可以通过表格 API 强制刷新此 BigQuery 数据?可以通过表格 UI(有一个刷新按钮)执行此操作,但我想通过调用 API 的其他一些服务来执行此操作。

【问题讨论】:

    标签: google-sheets google-bigquery


    【解决方案1】:

    您可以使用 Apps 脚本与电子表格中的数据连接器进行交互。

    注意事项

    您无法通过 Sheet API 操作数据源。您可以改用 Apps 脚本。设置刷新 BigQuery 数据的脚本相对容易。

    方法

    如果您想使用外部 API 执行此操作,则必须使用 Apps 脚本作为代理来实现您的目标。您可以将您的 Apps 脚本部署为 API 可执行文件,并从外部服务触发其功能。

    应用脚本

    /** @OnlyCurrentDoc */
    
    function refresh() {
      var spreadsheet = SpreadsheetApp.getActive();
      spreadsheet.getRange('A1').activate();
      spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Data Sheet 1'), true);
      SpreadsheetApp.enableAllDataSourcesExecution();
      spreadsheet.getCurrentCell().getDataSourceTables()[0].refreshData();
    };
    

    您的外部服务

    // [...] Oauth and other set ups...
    
    public static void main(String[] args) throws IOException {
        // ID of the script to call. Acquire this from the Apps Script editor,
        // under Publish > Deploy as API executable.
        String scriptId = "ENTER_YOUR_SCRIPT_ID_HERE";
        Script service = getScriptService();
    
        // Create an execution request object.
        ExecutionRequest request = new ExecutionRequest()
                .setFunction("refresh");
    
        try {
            // Make the API request.
            Operation op =
                    service.scripts().run(scriptId, request).execute();
    
    // [...] Error handling...
    

    参考:

    Big Query data connector

    Apps Script API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 2011-01-27
      相关资源
      最近更新 更多