使用谷歌应用脚​​本将单元格格式设置为文本 [英] Set cell format to text with google apps script

查看:82
本文介绍了使用谷歌应用脚​​本将单元格格式设置为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用谷歌应用程序脚本将数字格式设置为单元格,如

  cell.setNumberFormat(0.000) ; 

我需要将单元格格式设置为文本,以便任何类似日期的值不会自动转换。
我该怎么做? setNumberFormat 功能没有完整记录。此函数接受单个字符串中的大量参数,可用于指定一系列单元格的格式。



所以这里是关于如何设置setNumberFormat函数。



作为单个单元格的纯文本:

  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheets()[0];

//单个单元格
var cell = sheet.getRange(B2);

//纯文本
cell.setNumberFormat(@);

截止日期为单列:

  //单列
var column = sheet.getRange(B2:B);

//简单日期格式
column.setNumberFormat(M / d / yy);

作为整张表的货币:

  //整个工作表
var range = sheet.getRange(1,1,sheet.getMaxRows(),sheet.getMaxColumns());

//金钱格式
range.setNumberFormat($#,## 0.00; $(#,## 0.00));

来源:使用Google Apps脚本进行单元格格式设置


It is possible to set number format to a cell with google apps script like

cell.setNumberFormat("0.000");

I need to set cell format to a text, so that any date-like values were not automatically converted.
How can I do this?

解决方案

The setNumberFormat function in the Google Apps Script is not well documented. This function accepts a wide range of parameters in a single string that can be used to specify the format for a range of cells.

So here is the different example on how to set the setNumberFormat function.

As plain text for a single cell:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Single cell
var cell = sheet.getRange("B2");

// Plain text
cell.setNumberFormat("@");

As date for single column:

// Single column
var column = sheet.getRange("B2:B");

// Simple date format
column.setNumberFormat("M/d/yy");

As currency for entire sheet:

// Entire sheet
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());

// Money format
range.setNumberFormat("$#,##0.00;$(#,##0.00)");

Source: Cell Number Formatting with Google Apps Script

这篇关于使用谷歌应用脚​​本将单元格格式设置为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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