Class DataTableBuilder
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
DataTableBuilder
ตัวสร้างออบเจ็กต์ DataTable การสร้างตารางข้อมูลประกอบด้วยการระบุคอลัมน์ก่อน แล้วจึงเพิ่มแถวทีละแถว ตัวอย่าง
const data = Charts.newDataTable()
.addColumn(Charts.ColumnType.STRING, 'Month')
.addColumn(Charts.ColumnType.NUMBER, 'In Store')
.addColumn(Charts.ColumnType.NUMBER, 'Online')
.addRow(['January', 10, 1])
.addRow(['February', 12, 1])
.addRow(['March', 20, 2])
.addRow(['April', 25, 3])
.addRow(['May', 30, 4])
.build();
เอกสารประกอบโดยละเอียด
addColumn(type, label)
เพิ่มคอลัมน์ลงในตารางข้อมูล ระบบจะเพิ่มคอลัมน์จาก 0 ถึง n
แผนภูมิมักใช้คอลัมน์แรกสำหรับป้ายกำกับ (เช่น ป้ายกำกับแกน X ในแผนภูมิเส้น หรือป้ายกำกับส่วนในแผนภูมิวงกลม) ส่วนคอลัมน์อื่นๆ มักใช้สำหรับข้อมูล จึงมักต้องใช้ค่าตัวเลข
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
type | ColumnType | ประเภทข้อมูลในคอลัมน์ (ตัวเลข สตริง หรือวันที่) |
label | String | ป้ายกํากับของคอลัมน์ (ใช้สำหรับคำอธิบายแผนภูมิ) |
รีเทิร์น
DataTableBuilder
— ตัวสร้างนี้สําหรับการต่อเชื่อม
addRow(values)
เพิ่มแถวลงในตารางข้อมูล
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
values | Object[] | ค่าของแถวที่ระบุตามลําดับเดียวกับที่ป้อนคอลัมน์ |
รีเทิร์น
DataTableBuilder
— ตัวสร้างนี้สําหรับการต่อเชื่อม
build()
สร้างและแสดงตารางข้อมูล
รีเทิร์น
DataTable
— ตารางข้อมูล
โยน
Error
— หากตารางข้อมูลว่างเปล่าหรือมีรูปแบบไม่ถูกต้อง
setValue(row, column, value)
ตั้งค่าที่เฉพาะเจาะจงในตาราง
คุณสามารถกําหนดค่าก่อนเพิ่มคอลัมน์ลงในตารางข้อมูล อย่างไรก็ตาม ระบบจะไม่สนใจค่าดังกล่าว เว้นแต่จะมีการเพิ่มคอลัมน์ในภายหลัง
คุณไม่จำเป็นต้องกรอกค่าคอลัมน์ทั้งหมด ระบบจะถือว่ารายการที่ขาดหายไปเป็นnull
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
row | Integer | ดัชนีแถว (แถวแรกมีดัชนี 0) |
column | Integer | ดัชนีคอลัมน์ (คอลัมน์แรกมีดัชนี 0) |
value | Object | ค่าของเซลล์ตาราง (ควรมีประเภทที่ถูกต้องสำหรับคอลัมน์) |
รีเทิร์น
DataTableBuilder
— ตัวสร้างนี้สําหรับการต่อเชื่อม
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eDataTableBuilder facilitates the creation of DataTable objects by defining columns and adding rows sequentially.\u003c/p\u003e\n"],["\u003cp\u003eColumns are defined using \u003ccode\u003eaddColumn\u003c/code\u003e with specified data type and label, and rows are added using \u003ccode\u003eaddRow\u003c/code\u003e with corresponding values.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ebuild\u003c/code\u003e method finalizes the DataTable construction and returns the resulting object.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003esetValue\u003c/code\u003e allows modification of individual cell values within the table by specifying row, column, and the new value.\u003c/p\u003e\n"],["\u003cp\u003eDataTableBuilder uses a chaining pattern, where method calls can be linked for streamlined table construction.\u003c/p\u003e\n"]]],[],null,["# Class DataTableBuilder\n\nDataTableBuilder\n\nBuilder of DataTable objects. Building a data table consists of first specifying its columns, and\nthen adding its rows, one at a time. Example:\n\n```javascript\nconst data = Charts.newDataTable()\n .addColumn(Charts.ColumnType.STRING, 'Month')\n .addColumn(Charts.ColumnType.NUMBER, 'In Store')\n .addColumn(Charts.ColumnType.NUMBER, 'Online')\n .addRow(['January', 10, 1])\n .addRow(['February', 12, 1])\n .addRow(['March', 20, 2])\n .addRow(['April', 25, 3])\n .addRow(['May', 30, 4])\n .build();\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------------------------------|-------------------------------------------------------|-------------------------------------|\n| [addColumn(type, label)](#addColumn(ColumnType,String)) | [DataTableBuilder](#) | Adds a column to the data table. |\n| [addRow(values)](#addRow(Object)) | [DataTableBuilder](#) | Adds a row to the data table. |\n| [build()](#build()) | [DataTable](/apps-script/reference/charts/data-table) | Builds and returns a data table. |\n| [setValue(row, column, value)](#setValue(Integer,Integer,Object)) | [DataTableBuilder](#) | Sets a specific value in the table. |\n\nDetailed documentation\n----------------------\n\n### `add``Column(type, label)`\n\nAdds a column to the data table. Columns will be added from 0 to n.\n\nThe first column is often used by charts for labels (for instance, X-axis labels on line\ncharts, or slice labels in pie charts). The other columns are often used for data and therefore\noften require numeric values.\n\n#### Parameters\n\n| Name | Type | Description |\n|---------|---------------------------------------------------------|------------------------------------------------------|\n| `type` | [ColumnType](/apps-script/reference/charts/column-type) | type of data in the column (number, string, or date) |\n| `label` | `String` | label of the column (it's used for chart legends). |\n\n#### Return\n\n\n[DataTableBuilder](#) --- this builder, for chaining.\n\n*** ** * ** ***\n\n### `add``Row(values)`\n\nAdds a row to the data table.\n\n#### Parameters\n\n| Name | Type | Description |\n|----------|------------|-------------------------------------------------------------------------------|\n| `values` | `Object[]` | values for the row, specified in the same order that the columns are entered. |\n\n#### Return\n\n\n[DataTableBuilder](#) --- this builder, for chaining.\n\n*** ** * ** ***\n\n### `build()`\n\nBuilds and returns a data table.\n\n#### Return\n\n\n[DataTable](/apps-script/reference/charts/data-table) --- the data table\n\n#### Throws\n\n\n[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the data table is empty or otherwise malformed\n\n*** ** * ** ***\n\n### `set``Value(row, column, value)`\n\nSets a specific value in the table.\n\nYou may set a value before adding the column to the data table. However, unless the column\nis added at some point, the value will be ignored.\n\nNot all column values need to be filled in. Those missing will be considered `null`.\n\n#### Parameters\n\n| Name | Type | Description |\n|----------|-----------|--------------------------------------------------------------------------|\n| `row` | `Integer` | the row index (the first row has index 0) |\n| `column` | `Integer` | the column index (the first column has index 0) |\n| `value` | `Object` | the value of the table cell (should have the right type for the column). |\n\n#### Return\n\n\n[DataTableBuilder](#) --- this builder, for chaining"]]