【问题标题】:Importing CSV to Google SpreadSheet but I need an additional column in the middle of the csv array将 CSV 导入 Google 电子表格,但我需要在 csv 数组中间添加一列
【发布时间】:2013-11-22 15:53:18
【问题描述】:

我在下面列出了读取 csv 文件并将其放入要推送到电子表格的数组中的代码。但是,我将要读取的 csv 文件需要在中间增加一列以匹配电子表格中的数据集。电子表格和 csv 无法更改。感谢您的帮助!

// Import function
function importFromCSV() {

 var fileName = Browser.inputBox("Enter the name of the file in your Docs List to          import (e.g. myFile.csv):");

 var files = DocsList.getFiles();
  var csvFile = "";

  for (var i = 0; i < files.length; i++) {
if (files[i].getName() == fileName) {
  csvFile = files[i].getContentAsString();
  break;
    }
  }
  var csvData = CSVToArray(csvFile, ",");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Archive");
  for (var i = 0; i < csvData.length; i++) {
    sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));

  }
  var rowtoDel=csvData.length;
 delrowFunction(rowtoDel)
 Browser.msgBox("Jobs Successfully Imported.");
}




function CSVToArray( strData, strDelimiter ){
  // Check to see if the delimiter is defined. If not,
  // then default to comma.
  strDelimiter = (strDelimiter || ",");

  // Create a regular expression to parse the CSV values.
  var objPattern = new RegExp(
    (
      // Delimiters.
      "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

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

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


  // Create an array to hold our data. Give the array
  // a default empty first row.
  var arrData = [[]];

  // Create an array to hold our individual pattern
  // matching groups.
  var arrMatches = null;


  // Keep looping over the regular expression matches
  // until we can no longer find a match.
  while (arrMatches = objPattern.exec( strData )){

    // Get the delimiter that was found.
    var strMatchedDelimiter = arrMatches[ 1 ];

// Check to see if the given delimiter has a length
// (is not the start of string) and if it matches
// field delimiter. If id does not, then we know
// that this delimiter is a row delimiter.
if (
  strMatchedDelimiter.length &&
  (strMatchedDelimiter != strDelimiter)
){

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

}


// Now that we have our delimiter out of the way,
// let's check to see which kind of value we
// captured (quoted or unquoted).
if (arrMatches[ 2 ]){

  // We found a quoted value. When we capture
  // this value, unescape any double quotes.
  var strMatchedValue = arrMatches[ 2 ].replace(
    new RegExp( "\"\"", "g" ),
    "\""
  );

} else {

  // We found a non-quoted value.
  var strMatchedValue = arrMatches[ 3 ];

}


// Now that we have our value string, let's add
// it to the data array.
arrData[ arrData.length - 1 ].push( strMatchedValue );
 }



// Return the parsed data.
return( arrData );


}

【问题讨论】:

  • 如果将数据推送到临时工作表,添加列,然后复制到工作表中会怎样?
  • 这是个好主意,这可能是我可以做的解决方案。理想情况下,虽然我希望在读取数据或将数据输出到电子表格时以某种方式创建列。
  • 这有点麻烦...请参阅下面的答案。

标签: arrays csv google-apps-script spreadsheet


【解决方案1】:

在使用splice 方法写入工作表之前,您可以非常简单地在数组级别执行此操作。

请参阅下面的代码修改部分:

(我在本次测试中使用了第3列,可以改成任意有效数字,见代码中的cmets(小于数组宽度)

  for (var i = 0; i < csvData.length; i++) {
    var row = csvData[i]
    Logger.log(row);// see value before change
    row.splice(2,0,""); add an empty column on col C (arrays count from 0)
    Logger.log(row);// see value after change
    sheet.getRange(i+1, 1, 1, csvData[i].length).setValues([row]);
  }

【讨论】:

  • 这很完美!拼接方法正是我需要使用的。谢谢!
  • 不客气,请考虑接受答案。谢谢
猜你喜欢
  • 2013-11-05
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
相关资源
最近更新 更多