Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That is a nice specification, a great place to start! </p> <p>Remember that Google Apps Script is, to a large extent, JavaScript. If you extend your searching into JavaScript solutions, you'll find <a href="https://stackoverflow.com/search?q=%5Bjavascript%5D%20sort%20array%20multiple%20fields">plenty of examples of array sorts</a> here on SO.</p> <p>As it happens, much of what you need is in <a href="https://stackoverflow.com/questions/14569747/script-to-copy-and-sort-form-submission-data/20485809#20485809">Script to copy and sort form submission data</a>. You don't need the trigger part, but the approach to sorting can be easily adapted to handle multiple columns.</p> <p>The workhorse here is the comparison function-parameter, which is used by the JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="nofollow noreferrer">Array.sort() method</a>. It works through the three columns you've indicated, with ascending or descending comparisons. The comparisons used here are OK for Strings, Numbers and Dates. It could be improved with some cleaning up, or even generalized, but it should be pretty fast as-is.</p> <pre><code>function sortMySheet() { var sheet = SpreadsheetApp.getActiveSheet(); var dataRange = sourceSheet.getDataRange(); var data = dataRange.getValues(); var headers = data.splice(0,1)[0]; // remove headers from data data.sort(compare); // Sort 2d array data.splice(0,0,headers); // replace headers // Replace with sorted values dataRange.setValues(data); }; // Comparison function for sorting two rows // Returns -1 if 'a' comes before 'b', // +1 if 'b' before 'a', // 0 if they match. function compare(a,b) { var priorityCol = 0; // Column containing "Priority", 0 is A var openCol = 1; var projectCol = 2; // First, compare "Priority" A &gt; Z var result = (a[priorityCol] &gt; b[priorityCol] ) ? (a[priorityCol] &lt; b[priorityCol] ? -1 : 0) : 1; if (result == 0) { // "Priority" matched. Then compare "Open" Z &gt; A result = (b[openCol] &gt; a[openCol] ) ? (b[openCol] &lt; a[openCol] ? -1 : 0) : 1; } if (result == 0) { // "Open" matched. Finally, compare "Project" A &gt; Z result = (a[projectCol] &gt; b[projectCol] ) ? (a[projectCol] &lt; b[projectCol] ? -1 : 0) : 1; } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload