Cell Value Types and Number Formats — Strings, Numbers, Dates, Times, Booleans
level: basic / verified: 2026-10-01 / tabs: Node.js・Python・Java・WebAssembly / 日本語
What you want to do
Use the correct setter for each type of value you put into a cell (string / number / date / time / boolean), specify number formats, and understand the difference between getValue() and getValue(true).
Type quick reference
| Value to store | Method | Example |
|---|---|---|
| String | setValue(str) | setValue("Apple") |
| Number | setNumberValue(num) | setNumberValue(1234.5) |
| Date / time | setDateValue(dateTimeObject) | setDateValue(d) |
| Date / time (from a string) | setDateStringValue(str) | setDateStringValue("2024-01-15") |
| Boolean | setBooleanValue(bool) | setBooleanValue(true) |
| Let the type be detected automatically | setValue(str) | setValue("2024/1/15") → becomes a date |
| Formula | setFormula(str) | setFormula("SUM(A1:A3)") |
All setters share the same second and third arguments (forceString?, numberFormat?) (setDateStringValue / setValue included).
Code
Output:
// value-types.js — per-type setters and number formats, raw vs. display values
const { nodeosbxl, dto } = require("nodeosbxl");
const app = new nodeosbxl.App();
const wb = app.createWorkBook("value-types.xlsx");
const ws = wb.openWorkSheet("Sheet1");
const show = (a1) => {
const r = ws.getRange(`${a1}:${a1}`);
console.log(` ${a1}: disp=${JSON.stringify(r.getValue()[a1])}` +
` raw=${JSON.stringify(r.getValue(true)[a1])}` +
` fmt=${JSON.stringify(r.getNumberFormat()[a1])}`);
};
// --- dates & times ---
// Create a DateTimeObject with no arguments, then set its contents with setYMD() / setHMS()
const date = new dto.DateTimeObject();
date.setYMD(2024, 1, 15);
const datetime = new dto.DateTimeObject();
datetime.setYMD(2024, 1, 15);
datetime.setHMS(13, 45, 30);
const time = new dto.DateTimeObject();
time.setHMS(9, 5, 0);
console.log("[Dates & times]");
ws.getRange("A1:A1").setDateValue(date); // date only
ws.getRange("A2:A2").setDateValue(datetime); // date + time
ws.getRange("A3:A3").setDateValue(time); // time only
ws.getRange("A4:A4").setDateStringValue("2024-01-15"); // from a string
ws.getRange("A5:A5").setDateStringValue("令和6年1月15日"); // Japanese era (wareki) dates are recognized too
ws.getRange("A6:A6").setDateValue(date, false, "yyyy-mm-dd"); // number format can be given at the same time
ws.getRange("A7:A7").setDateValue(date, false, "ggge年m月d日"); // wareki + unquoted Japanese literals also work
["A1", "A2", "A3", "A4", "A5", "A6", "A7"].forEach(show);
// --- numbers & strings ---
console.log("[Numbers & strings]");
ws.getRange("B1:B1").setNumberValue(1234567.891);
ws.getRange("B2:B2").setNumberValue(1234567.891, false, "#,##0.00");
ws.getRange("B3:B3").setNumberValue(0.25, false, "0.0%");
ws.getRange("B4:B4").setValue("00123"); // leading zeros are kept as a string
ws.getRange("B5:B5").setValue("2024/1/15"); // the generic setter auto-detects dates
ws.getRange("B6:B6").setValue("12345", true); // forceString=true forces string handling
ws.getRange("B7:B7").setNumberValue(1234.5, false, '"円"#,##0'); // quoted literal ("yen")
ws.getRange("B8:B8").setNumberValue(-1234.5, false, "#,##0;赤-#,##0"); // bare color token (red) → [Red]
["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8"].forEach(show);
// --- booleans ---
console.log("[Booleans]");
ws.getRange("C1:C1").setBooleanValue(true);
ws.getRange("C2:C2").setBooleanValue(false);
ws.getRange("C3:C3").setFormula("TRUE"); // a formula can produce a boolean too
["C1", "C2", "C3"].forEach(show);
// --- date serial values ---
console.log("[Serial values]");
console.log(" serial value of 2024/1/15 =", app.getNumericValue(date));
console.log(" with the 1904 date system =", app.getNumericValue(date, true));
console.log(" wb.isDate1904() =", wb.isDate1904());
wb.save();
wb.close();
[Dates & times]
A1: disp="2024/1/15" raw="45306" fmt="yyyy/m/d;@"
A2: disp="2024/1/15 13:45:30" raw="45306.573263888888" fmt="yyyy/m/d h:mm:ss;@"
A3: disp="9:05:00" raw="0.37847222222222221" fmt="h:mm:ss;@"
A4: disp="2024/1/15" raw="45306" fmt="yyyy/m/d;@"
A5: disp="2024/1/15" raw="45306" fmt="yyyy/m/d;@"
A6: disp="2024-01-15" raw="45306" fmt="yyyy-mm-dd"
A7: disp="令和6年1月15日" raw="45306" fmt="ggge年m月d日"
[Numbers & strings]
B1: disp="1234567.891" raw="1234567.8910000001" fmt="General"
B2: disp="1,234,567.89" raw="1234567.8910000001" fmt="#,##0.00"
B3: disp="25.0%" raw="0.25" fmt="0.0%"
B4: disp="00123" raw="00123" fmt="General"
B5: disp="2024/01/15" raw="45306" fmt="yyyy/mm/dd"
B6: disp="12345" raw="12345" fmt="General"
B7: disp="円1,235" raw="1234.5" fmt="\"円\"#,##0"
B8: disp="-1,235" raw="-1234.5" fmt="#,##0;赤-#,##0"
[Booleans]
C1: disp="TRUE" raw="TRUE" fmt="General"
C2: disp="FALSE" raw="FALSE" fmt="General"
C3: disp="TRUE" raw="TRUE" fmt="General"
[Serial values]
serial value of 2024/1/15 = 45306
with the 1904 date system = 43844
wb.isDate1904() = false
Notes
getValue() vs. getValue(true)
| Call | Returns | Use it for |
|---|---|---|
getValue() | Display value (string after applying the number format) | Report output, on-screen display, CSV export |
getValue(true) | Raw value (internally stored value; dates are serial values, formulas are results) | Calculation, comparison, numeric processing |
Both return a map like { "A1": "...", ... } and all values are strings. Convert with Number(...) when treating them as numbers.
const raw = Number(ws.getRange("A1:A1").getValue(true)["A1"]); // 45306
const disp = ws.getRange("A1:A1").getValue()["A1"]; // "2024/1/15"
Dates
- Create a
DateTimeObjectwith no arguments and set it withsetYMD()/setHMS(). You can also set the fields individually withsetYear()/setMonth()/setDay()/setHour()/setMinute()/setSecond(). - If you do not specify
numberFormat, a default format is set automatically depending on the contents.- Date only →
yyyy/m/d;@ - Time only →
h:mm:ss;@ - Date + time →
yyyy/m/d h:mm:ss;@
- Date only →
setDateStringValue()recognizes the same date strings Excel does."2024-01-15","2024/1/15 13:45:30"and even the Japanese era string"令和6年1月15日"go in as dates.app.getNumericValue(dateTimeObject)returns the serial value. Passtrueas the second argumentis1904to get the serial value in the 1904 date system.
Usewb.isDate1904()to check which system the book uses.
Numbers and strings
setValue()auto-detects the type."123"→ number,"2024/1/15"→ date,"00123"→ string (leading zero preserved),"12A"→ string.- Use the dedicated setter when you want the intended type for sure.
setNumberValue()for numbers; setforceStringtotrueto pin a value as a string. numberFormatcan be given as the setter's third argument at the same time. The result is the same as callingsetNumberFormat()afterwards.
Caveats
- Set the number format AFTER putting the value in. If you call
setDateValue()aftersetNumberFormat(), the date setter overwrites the format with the default (measured).// ✗ the format is lost ws.getRange("A1:A1").setNumberFormat("yyyy-mm-dd"); ws.getRange("A1:A1").setDateValue(date); // → format gets overwritten with "yyyy/m/d;@" // ✓ the format survives ws.getRange("A1:A1").setDateValue(date); ws.getRange("A1:A1").setNumberFormat("yyyy-mm-dd"); // ✓ or give it at the same time via the setter's third argument ws.getRange("A1:A1").setDateValue(date, false, "yyyy-mm-dd"); setDateStringValue()throws on strings it cannot recognize.To avoid the exception, passws.getRange("A1:A1").setDateStringValue("this is not a date"); // → exception: can't recognize input value as DateTimetrueas the second argumentforceString. When recognition fails, the value is then entered as a plain string.- Number formats also support Japanese text, currency symbols and color specs (verified 2026-09-30). Quoted literals (
"円"#,##0) and unquoted Japanese text (yyyy年m月d日) display as-is, and the Japanese era calendar (ggge年m月d日→令和6年1月15日) and¥#,##0also work.
Color specs accept all three spellings[Red]/[赤]/ bare赤, and all of them are stored as[Red]. Examples of verified formats: | Kind | Format | Example display | |---|---|---| | Date |yyyy/m/d/yyyy/mm/dd/yyyy-mm-dd/yy/m/d/m/d/d-mmm-yyyy|2024/1/15,2024-01-15,15-Jan-2024| | Date (Japanese) |yyyy年m月d日/ggge年m月d日|2024年1月15日/令和6年1月15日| | Number |General/#,##0/#,##0.00/0.000/0%/0.0%|1,235,1,234.57,25.0%| | Number (Japanese text & currency) |"円"#,##0/¥#,##0|円1,235/¥1,235| | Number (negative section) |#,##0;[Red]-#,##0/#,##0;赤-#,##0|-1,235(in red) | - Raw number values contain double representation error. The raw value of
1234567.891is"1234567.8910000001". For comparisons and display, usegetValue()(the display value) or round the number first. getFormula()results have no leading=. ForsetFormula("SUM(A1:A3)"),getFormula()returns"SUM(A1:A3)".
See also
- Index
- Previous: Working with books and worksheets / Next: Working with rows, columns and cell ranges
- Overview — object model and basic flow
- API reference: Range / DateTimeObject