如何使用自定义版本比较器对包含版本号列的Google表格进行整体排序
You’re exactly right—Google Sheets’ built-in sort options don’t support custom comparator functions directly. The solution is to load your entire table data into memory as a 2D array, sort the rows using your custom version logic, then write the sorted data back to the sheet. Here’s how to implement this step-by-step:
Step 1: Update Your Custom Comparator to Work with Rows
Your existing _compareVer function likely compares two version strings, but we need it to accept full row arrays and extract the version from the correct column. For example, if your version is in the first column (index 0 of the row array), adjust it like this:
function _compareVer(rowA, rowB) { // Extract version strings from the first column (adjust index if your version is elsewhere) const verA = rowA[0]; const verB = rowB[0]; // Example semantic version comparison (adjust logic to match your version format) const partsA = verA.split('.').map(Number); const partsB = verB.split('.').map(Number); for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) { const numA = partsA[i] || 0; const numB = partsB[i] || 0; if (numA > numB) return -1; // Return -1 for descending order if (numA < numB) return 1; // Return 1 for descending order } return 0; // Versions are equal }
This comparator breaks down version strings into numeric parts and compares them sequentially, ensuring proper semantic sorting (e.g., 2.10 comes after 2.9).
Step 2: Modify Your Sort Function to Handle Full Rows
Update your sortAnalyticsVersionsDesc function to read all rows, sort them as a group, and write the sorted data back to the sheet:
function sortAnalyticsVersionsDesc() { const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const dataRange = activeSpreadsheet.getRangeByName(MY_RANGE); // Validate the named range exists if (!dataRange) { throw new Error("Named range 'MY_RANGE' not found! Ensure it includes both version and valueA columns."); } // Read all rows into a 2D array (each sub-array is a row of data) let allRows = dataRange.getValues(); // Filter out rows where the version column is empty (adjust index if needed) allRows = allRows.filter(row => row[0] !== ""); // Sort the full rows using your custom comparator allRows.sort(_compareVer); // Write the sorted rows back to the original range dataRange.setValues(allRows); }
Key Notes:
- Named Range Setup: Ensure
MY_RANGEincludes every column you want to sort with the version column (e.g., both version and valueA). If you don’t use a named range, replacegetRangeByName(MY_RANGE)with a direct range reference likegetRange("A2:B100"). - Column Index Adjustment: If your version column isn’t the first column in the range, change the index in
row[0]to match its position (e.g.,row[1]for the second column). - Ascending vs Descending: To switch to ascending sort, swap the return values in
_compareVer(return1whennumA > numBand-1whennumA < numB).
Why This Works
By working with the full 2D array of your data, each row is treated as a single unit. When you sort the array using _compareVer, every column (including valueA) stays paired with its original version number. Writing the sorted array back to the sheet replaces the original data with the correctly ordered rows.
内容的提问来源于stack exchange,提问作者Elad Benda

