【问题标题】:How to synchronize Fusion Tables from Spreadsheet partially?如何从电子表格部分同步融合表?
【发布时间】:2015-08-27 09:34:11
【问题描述】:

我已设置:Google 表单 -> 电子表格 ->(通过脚本文件)->(Fusiontable。

在 fusiontables 中,我有三列 County、Number 和 Geometry(kml 数据)。 我想从包括县和号码的电子表格中更新融合表文件,但不是 kml 数据(它包含的电子表格字符太多)

我已经设法从电子表格同步 fusiontables,但它替换了所有内容,每一列,包括 kml 数据(上面没有任何内容)。

是否可以部分同步行或按列同步?

电子表格脚本文件代码是

/**
* AppsScript script to run in a Google Spreadsheet that synchronizes its
* contents with a Fusion Table by replacing all rows.
*/

// Replace with your Fusion Table's ID (from File > About this table)
var TABLE_ID = '1T89UzCsy3Kug9ZkrZMzj2BT2ncOAq6lPm5vhllRk';

// First row that has data, as opposed to header information
var FIRST_DATA_ROW = 2;

// True means the spreadsheet and table must have the same column count
var REQUIRE_SAME_COLUMNS = false;

/**
 * Replaces all rows in the Fusion Table identified by TABLE_ID with the
 * current sheet's data, starting at FIRST_DATA_ROW.
 */
function sync() {
  var tasks = FusionTables.Task.list(TABLE_ID);
  // Only run if there are no outstanding deletions or schema changes.
  if (tasks.totalItems == 0) {
    var sheet = SpreadsheetApp.getActiveSheet();
   var wholeSheet = sheet.getRange('A1:B318');
    var values = wholeSheet.getValues();
    if (values.length > 1) {
      var csvBlob = Utilities.newBlob(convertToCsv_(values),
          'application/octet-stream');
      FusionTables.Table.replaceRows(TABLE_ID, csvBlob,
         { isStrict: REQUIRE_SAME_COLUMNS, startLine: FIRST_DATA_ROW - 1 });
      Logger.log('Replaced ' + values.length + ' rows');
    }
  } else {
    Logger.log('Skipping row replacement because of ' + tasks.totalItems +
        ' active background task(s)');
  }
}


/**
 * Converts the spreadsheet values to a CSV string.
 * @param {Array} data The spreadsheet values.
 * @return {string} The CSV string.
 */
function convertToCsv_(data) {
  // See https://developers.google.com/apps-script/articles/docslist_tutorial#section3
  var csv = '';
  for (var row = 0; row < data.length; row++) {
    for (var col = 0; col < data[row].length; col++) {
      var value = data[row][col].toString();
      if (value.indexOf(',') != -1 ||
          value.indexOf('\n') != -1 ||
          value.indexOf('"') != -1) {
        // Double-quote values with commas, double quotes, or newlines
        value = '"' + value.replace(/"/g, '""') + '"';
        data[row][col] = value;
      }
    }
    // Join each row's columns and add a carriage return to end of each row
    // except the last
    if (row < data.length - 1) {
      csv += data[row].join(',') + '\r\n';
    }
    else {
      csv += data[row];
    }
  }
  return csv;
}

【问题讨论】:

  • 是的,有可能。如果你找到了这段代码,你尝试过修改什么但没有工作?

标签: google-apps-script google-fusion-tables


【解决方案1】:

您不应该使用Table.replaceRows,它用于更新整个表。您必须编写 sql UPDATE 指令,使用 Query.sql 传递特定列和 where 子句来覆盖您需要的列和行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多