【发布时间】:2013-06-17 18:46:22
【问题描述】:
尽管我在网上找到了 许多示例(语法多种多样),但我无法获得 Logger.log(String format, Object.... values):Logger 格式化。 Google's official doc for Logger 声明它使用 Java 格式字符串语法进行字符串转换,但我的示例代码不同意。我疯了吗?
环境:
Linux (Fedora 17)
Chrome latest (v27.0.1453.110)
Google Apps Script (I assume this engine is server-side...)
$sudo yum install google-chrome-stable.x86_64
Loaded plugins: langpacks, presto, refresh-packagekit
Repository google-chrome is listed more than once in the configuration
Package google-chrome-stable-27.0.1453.110-202711.x86_64 already installed and latest version
Nothing to do
示例代码:
function testLogger() {
var state = "this";
Logger.log("Will %s work?", state.toString());
Logger.log("Will '%s' work?", state.toString());
Logger.log("Will [%s] work?", state.toString());
Logger.log('Will "%s" work?', state.toString());
Logger.log('Will %s work?', state.toString());
Logger.log('Will [%s] work?', state.toString());
Logger.log("Will %s work?", state);
Logger.log("Will '%s' work?", state);
Logger.log("Will [%s] work?", state);
Logger.log('Will "%s" work?', state);
Logger.log('Will %s work?', state);
Logger.log('Will [%s] work?', state);
Logger.log('[%s]\t is today', Date());
Logger.log('[%s]\t is today', Date().toString());
}
输出:
[13-06-17 11:33:23:660 PDT] Starting execution
[13-06-17 11:33:23:664 PDT] Logger.log([Will %s work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:664 PDT] Logger.log([Will '%s' work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:665 PDT] Logger.log([Will [%s] work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:665 PDT] Logger.log([Will "%s" work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:665 PDT] Logger.log([Will %s work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:665 PDT] Logger.log([Will [%s] work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:665 PDT] Logger.log([Will %s work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:665 PDT] Logger.log([Will '%s' work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:665 PDT] Logger.log([Will [%s] work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:666 PDT] Logger.log([Will "%s" work?, [this]]) [0.0 seconds]
[13-06-17 11:33:23:666 PDT] Logger.log([Will %s work?, [this]]) [0.001 seconds]
[13-06-17 11:33:23:666 PDT] Logger.log([Will [%s] work?, [this]]) [0.001 seconds]
[13-06-17 11:33:23:666 PDT] Logger.log([[%s] is today, [Mon Jun 17 2013 11:33:23 GMT-0700 (PDT)]]) [0.001 seconds]
[13-06-17 11:33:23:666 PDT] Logger.log([[%s] is today, [Mon Jun 17 2013 11:33:23 GMT-0700 (PDT)]]) [0.001 seconds]
[13-06-17 11:33:23:667 PDT] Execution succeeded [0.002 seconds total runtime]
【问题讨论】:
-
顺便提一下,我当然也搜索过 SO 但没有找到答案。
-
它对我有用。我已将结果粘贴到此处,但它在评论中是一个不可读的 blob。
-
@Fred:谢谢。您能否确认哪种语法有效?我的每一个示例测试都对你有用吗,也就是说,%s 被正确替换了吗?
-
我复制并粘贴了整个内容,它们都有效。