Google Apps Script runtime Chrome V8 causes error in and Utilities.parseCsv(csv, delimiter) when delimiter is ' ' (space)

Steffen :

In Google App script, I need to split a string using space as delimiter. I have used Utilities.parseCsv. Worked fine.

Then, I switched my script code to the new V8 runtime and encountered a bug with the parse.CSV function. This simple code below fails in V8, works fine in the legacy runtime. Error is "Exception: Cannot convert '' to char.".

I like to use the V8 runtime for the reasons so need to get this working. Does anybody have a workaround for that, please?

Thank you very much!

function test(){
var csvString = "Prefix Middle Suffix";
var data = Utilities.parseCsv(csvString, ' ');
Logger.log(data);
}

Tanaike :
  • You want to use Utilities.parseCsv(csv, delimiter) under V8.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

I think that this might be one of bugs for V8. Although I think that this might be resolved in the future update. As the current workaround, from the error message of Exception: Cannot convert '' to char., how about the following modification?

From:

var data = Utilities.parseCsv(csvString, ' ');

To:

var data = Utilities.parseCsv(csvString, ' '.charCodeAt(0));

or

var data = Utilities.parseCsv(csvString, Utilities.newBlob(' ').getBytes());

Result:

[["Prefix","Middle","Suffix"]]

Note:

  • In above modified script, it can be used with and without enabling V8.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=7127&siteId=1