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

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

问题描述

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

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

我想这样做的原因是我想以美国日期格式 (MM/DD/YYYY) 显示日期.[我们可以假设 OP 的 Google 帐户中的语言环境设置为使用 DD/MM/YYYY,这是一种更常见的日期格式.–编辑.]

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

我像这样创建字符串:

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

... 返回美国格式的日期,06/13/2012,这正是我想要的.但是,当我将单元格的值设置为该字符串时:

... 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);

...根据我的区域设置将日期格式化到单元格中,13/06/2012,而不是美国格式.

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

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

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

推荐答案

在 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@');

因此以编程方式格式化文本的神奇价值是@STRING@"!

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

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

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