Google Apps Script Utilities.parseCsv() and replacement character - �

后端 未结 3 716
忘了有多久
忘了有多久 2021-02-10 14:28

I\'m working on a project that involves a csv file in Google Drive that is updated with new data every minute or so.

I\'ve built a spreadsheet dashboard to make the dat

3条回答
  •  [愿得一人]
    2021-02-10 14:46

    This may help but you will probably need to investigate whether it causes other difficulties with your data:

    I had the same problem importing a .txt file containing bank transaction data as the bank doesn't offer CSV download files. I found that the odd Characters are FFFD which appear to be inserted by the fileXYZ.getblob() method as substitutes for unrecognized characters for unrecognized codes, in my case these are replaced by spaces.

    my (very basic) solution once you have a file loaded into is as follows..

    function getBankTransactionFile(fileNameToGet) {
    // fileNameToGet is .txt and stored in folder specified in Var list
    
     var  inputFileFolderID = '0B2XXX insert your folder ID',
         fldrID = DriveApp.getFolderById(inputFileFolderID),
         theFileRetrieved = fldrID.getFilesByName('yourFileName'),
         csvFile, cntFiles = 0;
    
    // Even if it's only one file, must iterate a while loop in order to access the file. Google drive will allow multiple files of the same name.
      while (theFileRetrieved.hasNext()) {
        var fileXYZ = theFileRetrieved.next();
        cntFiles = cntFiles + 1;
        csvFile = Utilities.parseCsv(fileXYZ.getBlob()
                  .getDataAsString().replace('\uFFFD'," ",'g'), "\n")
        // Utilities.parseCsv(csv, delimiter) returns 2D array but the fileXYZ 
        // text loaded has FFFD characters inserted so these are substituted for
        // 'space' using the .replace method and 'g' for global flag
      }
      return csvFile;
    }
    

    I am brand new to GAS (converting from VBA) so pretty sure there is a more refined way but it works for my data.. note the \n is the newline character as the specified delimiter for my data. I found out what the odd character were by using Logger to show the raw data string, then extracting the code .charCodeAt(n) counting the characters to find n. As the .txt will import you can see what the unrecognized characters should be.. spaces in my case.

提交回复
热议问题