3
如果數據表包含一列「版本號」,那麼如何才能在該列上正確排序生成的TableChart?使用「版本號」列對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-Script的範圍內對這些「字符串」進行自定義排序?
相關文章:
- How to compare software version number using js? (only number)
- How to sort varchar column (SQL) that contains number, chars, characters?
- How Can I Sort A 'Version Number' Column Generically Using a SQL Server Query
這裏的代碼片段從Gustavo Moura解禁幾乎直接。
您可以試試我的版本here。
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;
}
感謝Frederic,但禁用排序不是一個選項在這裏 - '點擊排序'功能是通緝。 – Mogsdad
這看起來可能不太好,但是你可以讓發行版的每一部分#列:'.addRow([「12」,「。0」,「。1」,70,60]'。想想除此之外的任何東西 – Fred
感謝你的想法...我已經採取了一點,並創建了一個函數來標準化單個列中的版本號,例如,'12 .0.1'變成'012.000.000.0000 '這並不是我正在尋找的東西,但它確實滿足了能夠按該列進行排序的要求,但不幸的是,這些數字並不像他們在輸入中那樣出現,因此用戶發現它很容易混淆 – Mogsdad