当分隔符为''(空格)时,Google Apps脚本运行时Chrome V8会在and Utilities.parseCsv(csv,delimiter)中导致错误 [英] Google Apps Script runtime Chrome V8 causes error in and Utilities.parseCsv(csv, delimiter) when delimiter is ' ' (space)

查看:117
本文介绍了当分隔符为''(空格)时,Google Apps脚本运行时Chrome V8会在and Utilities.parseCsv(csv,delimiter)中导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google App脚本中,我需要使用空格作为分隔符来分割字符串.我已经使用了Utilities.parseCsv.工作正常.

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

然后,我将脚本代码切换到新的V8运行时,并遇到了parse.CSV函数的错误.下面的简单代码在V8中失败,在旧版运行时中运行良好.错误为异常:无法将"转换为char.".

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.".

出于某些原因,我喜欢使用V8运行时,因此需要使其正常工作.请问有人有解决方法吗?

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

非常感谢!

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

推荐答案

  • 您要在V8下使用Utilities.parseCsv(csv, delimiter).
    • 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.

      我认为这可能是V8的错误之一.尽管我认为将来的更新中可能会解决此问题.作为当前解决方法,根据Exception: Cannot convert '' to char.的错误消息,如何进行以下修改?

      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?

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

      收件人:

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

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

      结果:

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

      注意:

      • 在上面修改的脚本中,可以在启用和不启用V8的情况下使用它.
        • parseCsv(csv, delimiter)
        • charCodeAt()
        • newBlob(data)
        • getBytes()

        如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

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

        这篇关于当分隔符为''(空格)时,Google Apps脚本运行时Chrome V8会在and Utilities.parseCsv(csv,delimiter)中导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆