可以将电子表格数据导出到JSON吗?

时间:2014-12-02 09:39:04

标签: google-drive-api

或任何其他可解析的格式,例如XML。目前,似乎只支持Excel,OO等电子表格格式。

2 个答案:

答案 0 :(得分:0)

无法直接完成,但以下的Apps脚本会将表格转换为电子表格中的JSON。然后可以导出JSON表,以便客户端应用程序可以下载它。

/**
 * Retrieves all the rows in the active spreadsheet that contain data and logs the
 * values for each row.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet

 NB Sheet 1 must be JSON and it is this sheet which is published as text to the app
 Sheet 2 is the Java string version of Sheet 1

 */
function readRows() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    Logger.log(row);
  }
};


var jsonOutput="";

/**
* main function
*/ 
function createJson() {
  jsonOutput = "{nEwLiNe";

  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var startSheet = 2;
  for (var i = startSheet; i < sheets.length; i++) {  // foreach sheet (except first two)
    if (i > startSheet) {
      jsonOutput+=",";
    }
    doSheet(sheets[i]);
  }
  jsonOutput += "nEwLiNe}";
  Logger.log("nEwLiNenEwLiNe==nEwLiNenEwLiNenEwLiNe"+jsonOutput);
  // write the output to A1
  SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange("A1").setValue(jsonOutput.replace(/nEwLiNe/g," "));

  // write a Java string to B1
  var java='jsonS = "'+jsonOutput.replace(/"/g,'\\"').replace(/nEwLiNe/g,' ')+'";';
  SpreadsheetApp.getActiveSpreadsheet().getSheets()[1].getRange("A1").setValue(java);

};


/**
* for each sheet
*/ 
function doSheet(sheet) {
  jsonOutput+="nEwLiNe";
  jsonOutput+='"'+sheet.getName()+'" : '
  var rows = sheet.getDataRange();
  var values = rows.getValues();
  var rowCount=values.length;

  // look for a blank column which is the end of columns to process,ie any extra columns to the right are ignored
  var colCount=values[0].length;
  for (var c = 0; c < colCount; c++) {
//    Logger.log(values[0][c]);
    if (values[0][c] == "" || values[0][c] == null) {
      colCount=c;
      break;
    }
  }

  if (rowCount > 2) {
    jsonOutput+="nEwLiNe[";
  }

  for (var r = 1; r < rowCount; r++) {  // for each data row
    if (r>1) {
      jsonOutput+=',';
    }
    jsonOutput+='nEwLiNe{nEwLiNe';
    for (var c = 0; c < colCount; c++) {
      if (c==0) {
        jsonOutput+='nEwLiNe';
      } else {
        jsonOutput+=',nEwLiNe';
      }
      var n=values[0][c].replace(/^\s+|\s+$/g, "");
      var v=(""+values[r][c]).replace(/^\s+|\s+$/g, "");
      v=v.replace(/\n/g,"").replace(/\r/g,"");
//      Logger.log(sheet.getName()+":"+r+" "+n+":"+v);
      jsonOutput+='    "'+n+'" : "'+v+'"'
    }
    jsonOutput+='nEwLiNe}nEwLiNe';
  }
  if (rowCount > 2) {
    jsonOutput+="nEwLiNe]nEwLiNe";
  }

};


/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the readRows() function specified above.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Create JSON",
    functionName : "createJson"
  }];
  sheet.addMenu("Tmph", entries);
};

答案 1 :(得分:0)

如果是Google表格,您可以使用基于列表的Feed或基于单元格的Feed,通过Google Sheet API(https://developers.google.com/google-apps/spreadsheets/)导出可解析的XML。