Interface DataTableSource
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
详细文档
getDataTable()
将此对象内的数据作为 DataTable 返回。
// Opens the spreadsheet file by its ID. If you created your script from a
// Google Sheets file, use SpreadsheetApp.getActiveSpreadsheet().
// TODO(developer): Replace the ID with your own.
const ss = SpreadsheetApp.openById('abc123456');
// Gets Sheet1 by its name.
const sheet = ss.getSheetByName('Sheet1');
// Gets the range A1:B7 on Sheet1.
const range = sheet.getRange('A1:B7');
// Gets the range A1:B7 as a data table. The values in each column must be of
// the same type.
const datatable = range.getDataTable();
// Uses the Charts service to build a bar chart from the data table.
// This doesn't build an embedded chart. To do that, use
// sheet.newChart().addRange() instead.
const chart = Charts.newBarChart()
.setDataTable(datatable)
.setOption('title', 'Your Chart Title Here')
.build();
返回
DataTable
- 数据以数据表格形式提供。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003eDataTableSource\u003c/code\u003e is an interface for objects that can present their data as a \u003ccode\u003eDataTable\u003c/code\u003e for use in Google Charts.\u003c/p\u003e\n"],["\u003cp\u003eClasses such as \u003ccode\u003eDataTable\u003c/code\u003e and \u003ccode\u003eRange\u003c/code\u003e implement the \u003ccode\u003eDataTableSource\u003c/code\u003e interface.\u003c/p\u003e\n"],["\u003cp\u003eThe primary method of \u003ccode\u003eDataTableSource\u003c/code\u003e is \u003ccode\u003egetDataTable()\u003c/code\u003e, which returns the object's data formatted as a \u003ccode\u003eDataTable\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou can access data from spreadsheet ranges and use it to create charts using the \u003ccode\u003eDataTableSource\u003c/code\u003e and the Charts service.\u003c/p\u003e\n"]]],["The `DataTableSource` interface allows objects to represent data as a `DataTable`. It includes the `getDataTable()` method, which returns the object's data in `DataTable` format. Classes like `DataTable` and `Range` implement this interface. The provided example demonstrates retrieving a `Range` from a spreadsheet, converting it to a `DataTable` using `getDataTable()`, and then utilizing this data to construct a bar chart with the Charts service.\n"],null,["# Interface DataTableSource\n\nDataTableSource\n\nInterface for objects that can represent their data as a [DataTable](/apps-script/reference/charts/data-table).\n\n### Implementing classes\n\n| Name | Brief description |\n|-------------------------------------------------------|---------------------------------------|\n| [DataTable](/apps-script/reference/charts/data-table) | A Data Table to be used in charts. |\n| [Range](../spreadsheet/range.html) | Access and modify spreadsheet ranges. |\n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------|-------------------------------------------------------|----------------------------------------------------|\n| [getDataTable()](#getDataTable()) | [DataTable](/apps-script/reference/charts/data-table) | Return the data inside this object as a DataTable. |\n\nDetailed documentation\n----------------------\n\n### `get``Data``Table()`\n\nReturn the data inside this object as a DataTable.\n\n```javascript\n// Opens the spreadsheet file by its ID. If you created your script from a\n// Google Sheets file, use SpreadsheetApp.getActiveSpreadsheet().\n// TODO(developer): Replace the ID with your own.\nconst ss = SpreadsheetApp.openById('abc123456');\n\n// Gets Sheet1 by its name.\nconst sheet = ss.getSheetByName('Sheet1');\n\n// Gets the range A1:B7 on Sheet1.\nconst range = sheet.getRange('A1:B7');\n\n// Gets the range A1:B7 as a data table. The values in each column must be of\n// the same type.\nconst datatable = range.getDataTable();\n\n// Uses the Charts service to build a bar chart from the data table.\n// This doesn't build an embedded chart. To do that, use\n// sheet.newChart().addRange() instead.\nconst chart = Charts.newBarChart()\n .setDataTable(datatable)\n .setOption('title', 'Your Chart Title Here')\n .build();\n```\n\n#### Return\n\n\n[DataTable](/apps-script/reference/charts/data-table) --- the data as a datatable."]]