2014-12-02 60 views

回答

0

不能直接完成,但以下Apps-script將在電子表格中將工作表轉換爲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); 
}; 
相關問題