Apps Script公式リファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2017年12月23日土曜日

数値の表示形式をスクリプトで変える


セルの数値の表示形式で「自動」「書式なしテキスト」などの書式をスクリプトで設定したくて書いたコードです

setNumberFormat()を使います


メニューから設定する場合は「表示形式」→「数字」から設定できるこの設定です


「書式なしテキスト」に設定したい場合

対象のセルを指定してsetNumberFormat("@")とすることで設定できます

function set_format() {
 SpreadsheetApp.getActiveSheet().getActiveCell().setNumberFormat("@");
}


試してみる


A1に 1234567890 と入力します


コード.gsのset_format()を実行して表示形式を見ると書式なしテキストに設定されています



それ以外の書式の設定


Date and Number Formats フォーマットの種類
https://developers.google.com/sheets/api/guides/formats


または


表示形式→数字→表示形式の詳細設定→カスタム数値形式...の中から指定したい形式を見つけて上のコードと同じく

function set_format() {
 SpreadsheetApp.getActiveSheet().getActiveCell().setNumberFormat("0.0");
}

のようにsetNumberFormat()の中に""で囲んで入れます


表示形式→数字→表示形式の詳細設定→カスタム数値形式...


おまけ


選択したセルの書式を知りたい場合はgetNumberFormat()を使います

function get_format() {
 var format = SpreadsheetApp.getActiveSheet().getActiveCell().getNumberFormat();
 Logger.log(format);
}



参考

Date and Number Formats フォーマットの種類
https://developers.google.com/sheets/api/guides/formats

getNumberFormat()
https://developers.google.com/apps-script/reference/spreadsheet/range#getNumberFormat()

setNumberFormat(String)
https://developers.google.com/apps-script/reference/spreadsheet/range#setNumberFormat(String)

スプレッドシートで数値の表示形式を設定する
https://support.google.com/docs/answer/56470?co=GENIE.Platform%3DDesktop&hl=ja

Latest post

Google Apps Scriptの障害時はIssueTrackerを見てみる - Incidents for Apps Script are reported on Issue Tracker

IssueTracker > Apps Script issues https://issuetracker.google.com/savedsearches/566234 Google Apps Scriptの障害時は IssueTracker に課題が上がっていることが...