Google Apps Script で CSV をシートに書き出す

function main() {  
    var spreadsheetObj = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = spreadsheetObj.getSheetByName("data");
    
    writeSheet(spreadsheetObj, sheet, csv.getDataAsString("Shift_JIS"), 1 ,1);
}

function writeSheet(spreadsheetObj, sheetObj, readData, startRow, startColumn) {
    "use strict";
    var csvData = readData.split('\n');
    var csvSplit = csvData[0].split(',');
    var maxColumn = csvSplit.length;
    var maxRow = csvData.length;
    var i, j, iLength, jLength;
    var logStr = "";
    var lineWriteArray = [];
    var srcRowArray = null;
    var range = null;
    var PROGRESS_BODER = 250;
    
    sheetObj.clear();
    
    expansionSheetSize(sheetObj, maxColumn + (startColumn - 1), maxRow + (startRow - 1));
    
    iLength = csvData.length;
    for (i = 0; i < iLength; i++) 
    {
        srcRowArray = getCsvSplit(csvData[i]);
        jLength = srcRowArray.length;
        for (j = 0; j < jLength; j++) {
            lineWriteArray.push(srcRowArray[j]);
        }
        range = sheetObj.getRange(startRow + i, startColumn, 1, lineWriteArray.length);
        range.setValues( [lineWriteArray] );
        lineWriteArray = [];
        if ( 0 === (i % PROGRESS_BODER) ) {
            logStr = " progress " + i + " / " + iLength;
            Logger.log(logStr);
            spreadsheetObj.toast(logStr);
        }
    }
    return true;
}

function expansionSheetSize(sheetObj, sizeColumn, sizeRow) {
    "use strict";
    var maxColumn = sheetObj.getMaxColumns();
    var maxRow = sheetObj.getMaxRows();
    var addColumnNum = sizeColumn - maxColumn;
    var addRowNum = sizeRow - maxRow;
    if(0 < addColumnNum) {
        sheetObj.insertColumns(maxColumn, addColumnNum);
    }
    if(0 < addRowNum) {
        sheetObj.insertRows(maxRow, addRowNum);
    }
}

function getCsvSplit(lineStr) {
    "use strict";
    var result = null;
    var tmpArray = null;
    var i = 0;
    var backup = lineStr;
    tmpArray = lineStr.split('"');
    if( 1 < tmpArray.length ) {
        lineStr = "";
        tmpArray[1] = this.replaceAll(tmpArray[1], ",", " ");
        for (i = 0; i < tmpArray.length; i++) {
            lineStr += tmpArray[i];
        }
    }
    result = lineStr.split(',');
    return result;
}