2014-09-24 169 views
0

我有一個二維數組,我試圖將其轉換爲CSV。我在網上發現了一些代碼,但是當我根據自己的需要調整它時,它不起作用。這是谷歌Apps腳本。二維數組爲CSV Javascript

不變的東西:

function saveAsCSV() { 
    // Prompts the user for the file name 
    var fileName = Browser.inputBox("Save CSV file as (e.g. myCSVFile):"); 

// Check that the file name entered wasn't empty 
if (fileName.length !== 0) { 
// Add the ".csv" extension to the file name 
fileName = fileName + ".csv"; 
// Convert the range data to CSV format 
var csvFile = convertRangeToCsvFile_(fileName); 
// Create a file in the Docs List with the given name and the CSV data 
DocsList.createFile(fileName, csvFile); 
} 
else { 
Browser.msgBox("Error: Please enter a CSV file name."); 
} 
} 

改變的東西:

function convertRangeToCsvFile_(csvFileName) { 
    // Get the selected range in the spreadsheet 
    var ws = SpreadsheetApp.getActiveSpreadsheet(); 
try { 
//var data = ws.getValues(); 
var csvFile = undefined; 

// Loop through the data in the range and build a string with the CSV data 
var csv = ""; 
var row = 3; 
csvFileName = "test9"; 
var datArray = Create2DArray(1,19); 

    datArray[0][0] = ws.getRange("A" + row.toString()).getValue().toString().toUpperCase(); 
    datArray[0][1] = ws.getRange("B"+row.toString()).getValue().toString().toUpperCase(); 
    datArray[0][2] = ws.getRange("C"+row.toString()).getValue().toString().toUpperCase(); 
    datArray[0][3] = ws.getRange("D"+row.toString()).getValue().toString().toUpperCase(); 
    datArray[0][4] = ws.getRange("E"+row.toString()).getValue().toString().toUpperCase(); 
    datArray[0][5] = ws.getRange("F"+row.toString()).getValue().toString().toUpperCase(); 
    datArray[0][6] = ws.getRange("G"+row.toString()).getValue().toString().toUpperCase(); 
    datArray[0][7] = ws.getRange("H"+row.toString()).getValue().toString().toUpperCase(); 
    datArray[0][8] = ws.getRange("I"+row.toString()).getValue().toString().toUpperCase(); 
    datArray[0][9] = new Date(ws.getRange("K"+row.toString()).getValue().toString()).getHours(); 
    datArray[0][10] = new Date(ws.getRange("K"+row.toString()).getValue().toString()).getMinutes(); 
    datArray[0][11] = new Date(ws.getRange("L"+row.toString()).getValue().toString()).getHours(); 
    datArray[0][12] = new Date(ws.getRange("L"+row.toString()).getValue().toString()).getMinutes(); 
    datArray[0][13] = new Date(ws.getRange("M"+row.toString()).getValue().toString()).getHours(); 
    datArray[0][14] = new Date(ws.getRange("M"+row.toString()).getValue().toString()).getMinutes(); 
    datArray[0][15] = new Date(ws.getRange("N"+row.toString()).getValue().toString()).getTime(); 
    datArray[0][16] = new Date(ws.getRange("N"+row.toString()).getValue().toString()).getFullYear(); 
    datArray[0][17] = new Date(ws.getRange("N"+row.toString()).getValue().toString()).getMonth(); 
    datArray[0][18] = new Date(ws.getRange("N"+row.toString()).getValue().toString()).getDate(); 
for(var i = 0; i < 19; i++){ 
    if(datArray[0][i] == ""){datArray[0][i] = "nil"; } 
} 
Logger.log(datArray); 
Logger.log(datArray.length); 

    // csv += datArray[row].join(",") + "\r\n"; 

    csv += datArray[0]; 
    Logger.log(csv); 
csvFile = csv; 

} 
catch(err) { 
Logger.log(err); 
Browser.msgBox(err); 
} 
} 

沒有錯誤顯示,但它顯示爲 「未定義」 爲CSV保存在谷歌驅動器。

+1

順便說一句,我在你的個人資料上看到你永遠不會接受任何答案......爲什麼?對你不夠好? (我不是在說我,更多的是關於bigQuery的問題。) – 2014-09-24 18:31:05

+0

不夠好,請檢查你的priveleges。 – 2014-09-29 15:20:42

回答

0

您的convertRangeToCsvFile_函數不返回任何東西。你需要一個return語句:

csvFile = csv; 
return csv; 

或者你可以定義變量csvFile作爲一個全局變量,如果你正試圖避免傳遞參數。

//Declared outside of a function 
var csvFile = ""; 

function saveAsCSV() { 
    code here 
);