appscript+bigquery+googlesheets卡顿和长时间更新文档
创始人
2024-11-10 06:30:32
0

1、使用App Script编写脚本来优化查询。通过使用App Script,可以将大数据量查询分段,并使用锁定以减少带宽占用量。

以下是使用App Script查询BigQuery并将结果写入Google Sheets的代码示例:

function runQuery() {
  var projectId = 'YOUR_PROJECT_ID';
  var request = {
    query: 'SELECT * FROM `bigquery-public-data.samples.gsod` LIMIT 10;'
  };
  var queryResults = BigQuery.Jobs.query(request, projectId);
  var jobId = queryResults.jobReference.jobId;

  // Check on status of the Query Job.
  var sleepTimeMs = 500;
  while (!queryResults.jobComplete) {
    Utilities.sleep(sleepTimeMs);
    sleepTimeMs *= 2;
    queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId);
  }

  // Get all the rows of results.
  var rows = queryResults.getRows();

  // Print column headers.
  var sheet = SpreadsheetApp.getActiveSheet();
  var headers = queryResults.getSchema().getFields();
  for (var i = 0; i < headers.length; i++) {
    sheet.getRange(1, i + 1).setValue(headers[i].name);
  }

  // Print rows of results.
  for (var rowNum = 0; rowNum < rows.length; rowNum++) {
    var values = rows[rowNum].f;
    for (var colNum = 0; colNum < headers.length; colNum++) {
      var cell = sheet.getRange(rowNum + 2, colNum + 1);
      cell.setValue(values[colNum].v);
    }
  }
}

2、使用Google Sheets的数据提取功能。将最近查询的结果存储到Google Sheets中,然后在新的查询中引用这些结果。

下面是一个使用数据提取功能的示例,它将BigQuery中存储的数据提取到Google Sheets中,并通过App Script将数据刷新成最新的数据:

function refreshData() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');

  // Get the current values.
  var currentValues = sheet.getDataRange().getValues();

  // Fetch the latest data from your BigQuery table.
  // `newData` should be a `2D array` of values.
  var newData = fetchNewData();

  // Replace the `currentValues` with `newData` if `currentValues` isn't empty.
  if (currentValues.length > 1) {

相关内容

热门资讯

透视了解!wpk透视辅助方法,... 透视了解!wpk透视辅助方法,wpk模拟器是什么-真是是有辅助插件(哔哩哔哩)1)有没有挂:进一步探...
透视揭幕!德普之星有透视辅助吗... 透视揭幕!德普之星有透视辅助吗,德普之星有透视辅助吗-切实一直总是有辅助攻略(哔哩哔哩)1、超多福利...
透视必备!wpk软件是正规的吗... 透视必备!wpk软件是正规的吗,wpk辅助购买-竟然一直都是有辅助工具(哔哩哔哩)1、不需要AI权限...
透视关于!wepoker免费透... 透视关于!wepoker免费透视脚本,wepoker有人用过吗-好像是真的有辅助插件(哔哩哔哩)1、...
透视教你!hhpoker德州挂... 透视教你!hhpoker德州挂真的有吗,hh poker辅助有用吗-竟然一直都是有辅助教程(哔哩哔哩...
透视分享!wepoker透视脚... 透视分享!wepoker透视脚本,wepoker免费透视-好像是真的有辅助脚本(哔哩哔哩)1、实时e...
透视专业!德普之星有辅助软件吗... 透视专业!德普之星有辅助软件吗,德普之星透视-真是是真的有辅助工具(哔哩哔哩)1、下载好透视辅助下载...
透视必备!wepoker免费脚... 透视必备!wepoker免费脚本弱密码,we-poker软件-本来有辅助教程(哔哩哔哩)1、下载好正...
透视揭幕!hhpoker辅助,... 透视揭幕!hhpoker辅助,hhpoker万能辅助器-一直有辅助软件(哔哩哔哩)1、在插件功能辅助...
透视科普!wepoker辅助插... 透视科普!wepoker辅助插件功能,we poker辅助器v3.3-切实是真的有辅助插件(哔哩哔哩...