2013-04-26 53 views
1

下面是从CSV导入的文本的一个原始行:Javascript和正则表达式来导入CSV文件

10,"2013-04-17 16:29:36",out,"BTC sold: [tid:0450750450454505] 0.85985758 BTC at 69.88355 € (0.55% fee)",0.85985758,0 

我想要编辑的代码是相当简单的,来自[本教程页] [ 1](*的主代码副本是在本页面底部):

主要的正则表达式的代码部分是:

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

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

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

我要的是,在我在开始时复制的原始行文字,我想删除或忽略什么位于括号括号()之间,并使单词“at”成为新列的分隔符。当我导入CSV时,新列的值将是“at”之后的值。

等效的regexp代码应该是: [(?)] |(?()) 和 (\蝙蝠\ B)

我试图像添加各种选项(\蝙蝠\ b)在分隔符部分的正则表达式中,但它不起作用。

非常感谢您的帮助!

整个代码:

function onOpen() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var csvMenuEntries = [{name: "Load from CSV file", functionName: "importFromCSV"}]; 
    ss.addMenu("CSV", csvMenuEntries); 
} 

function search() { 
    // Prompt the user for a search term 
    var searchTerm = Browser.inputBox("Enter the string to search for:"); 

    // Get the active spreadsheet and the active sheet 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getActiveSheet(); 

    // Set up the spreadsheet to display the results 
    var headers = [["File Name", "File Type", "URL"]]; 
    sheet.clear(); 
    sheet.getRange("A1:C1").setValues(headers); 

    // Search the files in the user's Docs List for the search term 
    var files = DocsList.find(searchTerm); 

    // Loop through the results and display the file name, file type, and URL 
    for (var i = 0; i < files.length; i++) { 
    sheet.getRange(i+2, 1, 1, 1).setValue(files[i].getName()); 
    sheet.getRange(i+2, 2, 1, 1).setValue(files[i].getType()); 
    if (files[i].getType() == "document") { 
     urlBase = "https://docs.google.com/Doc?docid="; 
    } 
    else if (files[i].getType() == "spreadsheet") { 
     urlBase = "https://spreadsheets.google.com/ccc?key="; 
    } 
    else if (files[i].getType() == "presentation") { 
     urlBase = "https://docs.google.com/present/view?id="; 
    } 
    else { 
     urlBase = "https://docs.google.com/fileview?id="; 
    } 
    sheet.getRange(i+2, 3, 1, 1).setValue(urlBase + files[i].getId()); 
    } 
} 

function importFromCSV() { 
    var fileName = "history_BTC.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.getActiveSheet(); 
    for (var i = 0; i < csvData.length; i++) { 
    sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i])); 
    } 
} 

// This will parse a delimited string into an array of 
// arrays. The default delimiter is the comma, but this 
// can be overriden in the second argument. 

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|\bat\b|\\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); 
} 
+0

你有没有考虑简单地使用'\ | \(?(*)\)'在输入字符串替换为“ “,然后用适当的分隔符替换'/ \ bat \ b/g','在你使用该函数进行处理之前 – 2013-04-26 11:12:13

回答

0

首先,将 “以” 用 “”。这不需要正则表达式。

然后,它是一个简单的替换:(。*?)\]

a=myString.replace(" at ", ","); 
b=a.replace(" at ", ","); 
c=b.replace(/(\[[^\]]+\]|\([^)]+\))/gi, ""); 
+0

我开始理解逻辑了,谢谢:)你能否告诉我粘贴这个的位置,请多多关注这个小时? – 2013-04-26 12:40:19

+0

还必须更改myString?我已经尝试了很多小时而没有成功。无论如何,感谢所有的帮助。 – 2013-04-26 14:04:48

+0

我仍然试图用dda的建议来完成这项工作,但是我不知道该把代码完全放在哪里。此外,我想我必须取代myString的东西,可能是csvData或csvFile,但我得到像“TypeError:无法找到函数替换对象”的错误,我敢肯定,提供的代码应该完美的工作,但我太糟糕了在JavaScript。 – 2013-04-26 18:50:42