2012-06-30 89 views

回答

1

我們發佈了a tutorial,演示瞭如何將CSV導入電子表格。

+1

本教程使用DocsList,它已被棄用且不再工作。 – marcp

+0

該鏈接有404。這是一個錯誤。「 –

5

這與DocList的錯誤答案類似,但不是使用DocsList,而是直接從blob中獲取數據,解析並將其導入電子表格。我只給出一個簡要概述:

function doGet(e) { 
    var app = UiApp.createApplication().setTitle("Upload CSV to Sheet"); 
    var formContent = app.createVerticalPanel(); 
    formContent.add(app.createFileUpload().setName('thefile')); 
    formContent.add(app.createSubmitButton('Start Upload')); 
    var form = app.createFormPanel(); 
    form.add(formContent); 
    app.add(form); 
// return app; 
    SpreadsheetApp.getActiveSpreadsheet().show(app);// show app 
} 

function doPost(e) { 
    // data returned is a blob for FileUpload widget 
    var fileBlob = e.parameter.thefile; 

    // parse the data to fill values, a two dimensional array of rows 
    // Assuming newlines separate rows and commas separate columns, then: 
    var values = [] 
    var rows = fileBlob.contents.split('\n'); 
    for(var r=0, max_r=rows.length; r<max_r; ++r) 
    values.push(rows[r].split(',')); // rows must have the same number of columns 

    // Using active sheet here, but you can pull up a sheet in several other ways as well 
    SpreadsheetApp.getActiveSheet() 
       .getRange(1, 1, values.length, values[0].length) 
       .setValues(values); 
} 
相關問題