给定一个包含“Release Numbers”列的数据表,如何在该列上正确排序生成的 TableChart?
这些是表示“版本号”的字母数字字符串的示例:
"1.0", "12.0.1", "2.10", "2.0.0.142", "2.0.0.1_2", "10.2"
此示例所需的排序顺序为:
"1.0", "2.0.0.2", "2.0.0.142", "2.10", "10.2", "12.0.1"
由于这些值的格式,它们必须被视为“字符串”,因此按字母顺序排序。这会导致列表不符合逻辑“发布”顺序:
"1.0", "10.2", "12.0.1", "2.0.0.142", "2.0.0.2", "2.10"
有许多比较值的解决方案,所以这不是这个问题的重点。相反,如何在 Google Apps 脚本的范围内对这些“字符串”进行自定义排序?
相关文章:
这是一个几乎直接来自Gustavo Moura的代码片段。
你可以在这里试试我的版本。
function doGet() {
// Populate the DataTable. We'll have the data labels in
// the first column, "Quarter", and then add two data columns,
// for "Income" and "Expenses"
var dataTable = Charts.newDataTable()
.addColumn(Charts.ColumnType.STRING, "Release")
.addColumn(Charts.ColumnType.NUMBER, "Income")
.addColumn(Charts.ColumnType.NUMBER, "Expenses")
.addRow(["1.0", 60, 55])
.addRow(["10.2", 50, 60])
.addRow(["2.10", 100, 50])
.addRow(["12.0.1", 70, 60])
.addRow(["2.0.0.142", 30, 50])
.addRow(["2.0.0.1_2", 114, 75])
.build();
// Build the chart.
var chart = Charts.newTableChart()
.setDataTable(dataTable)
// .setTitle("Income and Expenses per Release")
.build();
// Add our chart to the UI and return it so that we can publish
// this UI as a service and access it via a URL.
var ui = UiApp.createApplication();
ui.add(chart);
return ui;
}