将新的CSV数据附加到Google表格

时间:2016-01-04 12:50:45

标签: csv google-apps-script google-sheets google-apps

如何将导入的CSV数据附加到现有的Google电子表格中?导入后,底部有一个空行,随后每次运行代码时我都希望在该空行结束后插入新的CSV数据。

这是我现有的代码:

function getCSV() {
var fSource = DriveApp.getFolderById('0B2lVvlNIDosoajRRMUwySVBPNVE'); //       reports_folder_id = id of folder where csv reports are saved
var fi = fSource.getFilesByName('L661_BOM-CAD_14-12-15.csv'); // latest       report file
var ss    =SpreadsheetApp.openById('1WEDYfEudYsbkUhHbCxZspEbNXz3cjQIe3JdhnbFmmYA'); //      data_sheet_id = id of spreadsheet that holds the data to be updated with new      report data
var sd = SpreadsheetApp.getActiveSpreadsheet();
var s = SpreadsheetApp.getActiveSheet();
var target = new Array()
var sSheet = SpreadsheetApp.getActiveSpreadsheet();
sheet = SpreadsheetApp.setActiveSheet(sSheet.getSheets()[0]);

if ( fi.hasNext()) {
var file = fi.next();
var csv = file.getBlob().getDataAsString();
var csvData = CSVToArray(csv); // see below for CSVToArray function

var sheet = ss.getSheets()[0];

s.getRange(+1, 1, csvData.length, csvData[5].length).setValues(csvData);

}
};


function CSVToArray( strData, strDelimiter ){

strDelimiter = (strDelimiter || ";");

   var objPattern = new RegExp(
    (
        // Delimiters.
        "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

        // Quoted fields.
        "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

        // Standard fields.
        "([^\"\\" + strDelimiter + "\\r\\n]*))"
    ),
    "gi"
    );

var arrData = [[]];

var arrMatches = null;

while (arrMatches = objPattern.exec( strData )){


    var strMatchedDelimiter = arrMatches[ 1 ];
    if (
        strMatchedDelimiter.length &&
        strMatchedDelimiter !== strDelimiter
        ){

        // Since we have reached a new row of data,
        // add an empty row to our data array.
        arrData.push( [] );

    }

    var strMatchedValue;
    if (arrMatches[ 2 ]){
        strMatchedValue = arrMatches[ 2 ].replace(
            new RegExp( "\"\"", "g" ),
            "\""
            );

    } else {

        strMatchedValue = arrMatches[ 3 ];
    }

    arrData[ arrData.length - 1 ].push( strMatchedValue );
}
return( arrData );
  }

2 个答案:

答案 0 :(得分:1)

我设法通过添加getLastRow()来解决问题;命令

希望这可以帮助其他人

var lastrow = s.getLastRow();
s.getRange(lastrow + 1, 1, csvData.length, csvData[0].length).setValues(csvData);

答案 1 :(得分:0)

现在可以使用parseCSV实用工具简化此操作。如果将fSource,fi和ss变量的值传递给此修改后的函数,则该函数仍将起作用,并且不必使用CSVToArray方法。 感谢您指出正确的方向。默认情况下,您的电子表格应已加载到第一张工作表(0),因此我认为其他的“ getActiveSpreadsheet”和getActiveSheets是多余的。

function getCSVAndAppend(spreadsheetId, folderId, filename) {
  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFilesByName(filename);

  var openSpreadsheet = SpreadsheetApp.openById(spreadsheetId);
  var activeSheet = SpreadsheetApp.getActiveSheet();

  if ( files.hasNext()) {
    var file = files.next();
    var csv = file.getBlob().getDataAsString();
    var csvData = Utilities.parseCsv(csv);

    var lastrow = activeSheet.getLastRow();
    activeSheet.getRange(lastrow + 1, 1, csvData.length, csvData[0].length).setValues(csvData);
  }
}