Apps Script Utilities.parseCsv假定在双引号内的换行符上有新行 [英] Apps Script Utilities.parseCsv assumes new row on line breaks within double quotes
问题描述
使用 Utilities.parseCsv()
双引号内的换行符被假定为完全是新行。这个函数的输出数组会有几个不正确的行。
When using Utilities.parseCsv()
linebreaks encased inside double quotes are assumed to be new rows entirely. The output array from this function will have several incorrect rows.
如何解决这个问题,或者解决它?
How can I fix this, or work around it?
编辑:具体来说,我可以避免只存在于双引号内的换行符吗?即/
Specifically, can I escape line breaks that exist only within double quotes? ie.
/r/n "I have some stuff to do:/r/n Go home/r/n Take a Nap"/r/n
会被转换为:
Would be escaped to:
/r/n "I have some stuff to do://r//n Go home//r//n Take a Nap"/r/n
Edit2:2012年的错误报告: https://code.google.com/p/google-apps-script-issues/issues/detail?id=1871
Bug report from 2012: https://code.google.com/p/google-apps-script-issues/issues/detail?id=1871
推荐答案
在另一篇文章中检索并稍微修改另一个回复中的正则表达式:https://stackoverflow.com/a/29452781/3547347
Retrieved and slightly modified a regex from another reply on another post: https://stackoverflow.com/a/29452781/3547347
正则表达式:(?= ['])(?: [^ \\] *(?: \\ [\s\S] [^ \\] *)* | '[^' \\] \r\\\
(?:\\ [\s\S] [^ '\\] \r\\\
)*')
代码:
var dataString = myBlob.getDataAsString();
var escapedString = dataString.replace(/(?=["'])(?:"[^"\\]*(?:\\[\s\S][^"\\]*)*"|'[^'\\]\r\n(?:\\[\s\S][^'\\]\r\n)*')/g, '\\r\\n');
这篇关于Apps Script Utilities.parseCsv假定在双引号内的换行符上有新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!