Utilities.parseCsv() 'Could not parse text' (Google Apps Script)

后端 未结 3 2055
情书的邮戳
情书的邮戳 2021-01-07 03:32

I\'m really confused because this was working earlier and I don\'t know why it is broken now. I\'ve got CSV files that I\'m trying to parse. I\'m accessing them using the fo

相关标签:
3条回答
  • 2021-01-07 03:37

    I tried testing your function slight modified and it looks like it works okay. Here's the one I used. If you can share the data file with us, I'd be glad to take a look at the problem.

    function prrrrse() 
    {
      var br = '<br />';
      var a = 'test""123,456\n';
      var b = "abc' 'def,ghi\n";
      var h = 'x,y\n';
      var str = h+a+b;
      var delimiter = ',';
      var csv = Utilities.parseCsv(str,delimiter);
      var s = 'str=' + str + br + 'delimiter=' + delimiter + br;
      for(var i = 0;i<csv.length;i++)
      {
        s+='csv[' + i + ']=';
        for(var j=0;j<csv[i].length;j++)
        {
          if(j>0)s+=' , ';
          s+=csv[i][j];
        }
        s+= br;
      }
      dispStatus('Parsing',s, 800, 600); 
    }
    
    
    function dispStatus(title,html,width,height,modal)
    {
      var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
      var width = typeof(width) !== 'undefined' ? width : 400;
      var height = typeof(height) !== 'undefined' ? height : 300;
      var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
      var modal = typeof(modal) !== 'undefined' ? modal : false;
      var htmlOutput = HtmlService
         .createHtmlOutput(html)
         .setWidth(width)
         .setHeight(height);
     if(!modal)
     {
       SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
     }
     else
     {
       SpreadsheetApp.getUi().showModalDialog(htmlOutput, title);
     }
    } 
    
    0 讨论(0)
  • 2021-01-07 03:39

    For what it's worth, I just had a similar problem crop up, and after trying to find an errant comma, ended up with a simpler solution. Line 18,000-something (out of 20,000+) in the csv somehow had an extra line break (\n) in it. Eliminating the line break solved the problem.

    0 讨论(0)
  • 2021-01-07 04:00

    I was dealing with the same problem today and discovered that trailing spaces at the end of some lines were causing the parser to fail. I resolved this with this regular expression: csvString.replace(/[ \t]+$/gm, '')

    0 讨论(0)
提交回复
热议问题