使用电子表格中的数据以编程方式更新Google FusionTables

时间:2016-04-08 02:29:46

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

我正在尝试使用Google Spreadsheet中的数据更新FusionTables中五列中的四列。我刚刚开始编写Apps脚本。我需要更新查询的帮助。任何建议或反馈表示赞赏。先感谢您。

   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 data = sheet.getDataRange().getValues();
      //i = 1 for 1st row with values in spreadsheet.  0 for column headers.
      for (var i = 1; i < data.length; i++) {
          var cName = data[i][0];
          var ed_vs = data[i][1];
          var ed_vs_ada = data[i][2];
          var color = data[i][3];      
          //update ft
          var updateQry = "update "+TABLE_ID+" set ED_VS = "+ed_vs+",ED_VS_ADA = "+ed_vs_ada+", COLOR = "+color+ "where COUNTY = "+cName;
          //FusionTables.Query.sql(updateQry);
          //is this even possible to execute an update query? help with syntax? 
          Logger.log(updateQry);  
        }   
    }  
};

1 个答案:

答案 0 :(得分:0)

这是针对有类似问题的人的有效解决方案。

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 data = sheet.getDataRange().getValues();
    //i = 1 for 1st row with values in spreadsheet.  0 will give column headers.
    for (var i = 1; i < data.length; i++) {
      var cName = data[i][0];
      var ed_vs = data[i][1];
      var ed_vs_ada = data[i][2];
      var color = data[i][3];

      //parse rowid from output
      var selQry = "select ROWID FROM "+TABLE_ID+" where COUNTY = '"+cName+"' ";
      var cdata = FusionTables.Query.sql(selQry);
      var c_rowid = cdata.rows[0][0]; //rowid
      Logger.log(c_rowid);

      var updateQry = "update "+TABLE_ID+" set ED_VS = '"+ed_vs+"',ED_VS_ADA = '"+ed_vs_ada+"', COLOR = '"+color+ "' where ROWID = '"+c_rowid+"' ";

      FusionTables.Query.sql(updateQry);

      Logger.log(updateQry);         
    }   
  }
};