通过Apps脚本以明文格式化Google表格单元格 [英] Format a Google Sheets cell in plaintext via Apps Script

查看:162
本文介绍了通过Apps脚本以明文格式化Google表格单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Apps脚本,我希望将Google表格单元格的格式设置为纯文本格式。



我想这样做的原因是因为我想以美国日期格式(MM / DD / YYYY)显示日期。 [我们可以假定OP的Google帐户中的语言环境设置为使用DD / MM / YYYY,这是一种更常见的日期格式。我创建了这样的字符串:

  var thisDate = Utilities.formatDate(new Date(),SpreadsheetApp.getActiveSpreadsheet()。getSpreadsheetTimeZone(),MM / d / yyyy); 

...以美国格式返回日期 06/13/2012 ,这是我想要的。然而,当我设置一个单元格的值为该字符串:

$ pre $ sheet $ getCange(1,n)。 setValue(thisDate);

...根据我的语言环境将日期格式化为单元格, 13/06 / b>,而不是美国格式。

以下操作也失败了,因为日期是以标准格式返回的,而不是美国格式: p>

  sheet.getRange(1,n)。当单元格被格式化为纯文本时,getValue()==2012/6/13

而不是约会,一切工作正常。



所以,我的问题是,如何使用JavaScript来格式化单元格。

解决方案

  sheet.getRange(1,n).setNumberFormat('@ STRING @'); 

所以编程格式化文本的魔法值是'@STRING @'!


Using Google Apps Script, I want to set the format for a Google Sheets cell to plain text.

The reason I want to do this is because I want to show a date in the U.S. date format (MM/DD/YYYY). [We can assume the locale in the OP's Google account is set to use DD/MM/YYYY, a more common date format. –Ed.]

I create the string like this:

var thisDate = Utilities.formatDate (new Date (), SpreadsheetApp.getActiveSpreadsheet (). getSpreadsheetTimeZone (), "MM / d / yyyy");

... which returns the date in the U.S. format, 06/13/2012, which is what I want. However, when I set the value of a cell to that string:

sheet.getRange (1, n). setValue (thisDate);

... the date is formatted into the cell according to my locale, 13/06/2012, not the U.S. format.

The following operation also fails, because the date is returned in the standard format, not the U.S. format:

sheet.getRange (1, n). getValue () == "06/13/2012"

When the cell is formatted as plain text, and not a date, everything works fine.

So, my question is, how to format a cell using JavaScript.

解决方案

The other answer, to set the format to 'plain text' in javascript, doesn't work. However, this does:

sheet.getRange(1,n).setNumberFormat('@STRING@');

So the magic value for formatting text programmatically is '@STRING@'!

这篇关于通过Apps脚本以明文格式化Google表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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