Utilities.formatString()新的Apps脚本方法,无法按预期工作 [英] Utilities.formatString() New Apps Script method, not working as intended

查看:129
本文介绍了Utilities.formatString()新的Apps脚本方法,无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新方法:Utilities.formatString()

I am using the new method: Utilities.formatString()

Google文档中,它类似于sprintf%-style

In the Google Documentation is says it is similar to sprintf %-style.

我搜索并阅读了有关PHP中sprintf的本文.

I searched and read this article about sprintf in PHP.

我似乎无法使它按预期工作.这意味着用8个前导零填充这8个字符串.我知道还有其他方法可以执行此操作,但是我正在尝试获取有关sprintf/formatString的信息.

I cannot seem to get this to work as intended. It is meant to pad this 8 character string with 4 leading zeros. I know there are other ways to do this, But I am trying to get a handle on this sprintf / formatString thing.

var noFormat = "12345678";
var formatted = Utilities.formatString("%012s", noFormat);

我希望将var格式设置为等于"000012345678". 我的调试器告诉我formatted = 0,否则有时会引发错误.

I expected the var formatted to be equal to "000012345678". my debugger tell me that formatted = 0, or sometimes it throws an error..

我很困惑.

推荐答案

像这样尝试:

function xxx(){
  var noFormat = '12345678'
  var formatted = Utilities.formatString("%012d", noFormat);
Logger.log(formatted)
}

可以在网上轻松找到可以使用的不同参数,此处是一个示例,解释了如何在php中对参数进行求值,但用法相同.

the different parameters that can be used are easy to find on the web, here is an example that explains how the argument must be evaluated in php but the usage is the same.

记录器结果:

这篇关于Utilities.formatString()新的Apps脚本方法,无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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