Google Script sort troubleshooting (works as 2 functions but not combined)












1















I'm working with a google apps script to sort data in columns A-C by a number in column D. Column D's number is dependent on the data in Columns A-C (see below).



https://docs.google.com/spreadsheets/d/11QSZPAl3nCs7nzpZIR4FzjfkPUyBBut1pD2K0919UB8/edit?usp=sharing



I'm trying to make it so that there is a blank row up at the top (in A6:C6) so new data can be entered, then upon sorting it will move down into the main list (A7:C299) and a new blank row will appear in A6:C6 again.



I tried doing this by moving the new entry to the bottom of the sheet, then having it sort, but for some reason it doesn't sort the new entry once it has been moved. If I delete the code for the sort, it moves correctly, and if I just have the code to sort (after it has copy-pasted the data from A6:C6), it also works, but when the functions are combined it doesn't. Any ideas or another way to accomplish this? Thanks!



function SortList() {

var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange("A7:D299");

// Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

var copyrange = sheet.getRange("A6:C6");
var pasterange = sheet.getRange("A298:C298");
var copyvalues = copyrange.getValues();


pasterange.setValues(copyvalues);
copyrange.clearContent();

// Sorts by the values in the first column (A)
range.sort({column: 4, ascending: false});

}









share|improve this question



























    1















    I'm working with a google apps script to sort data in columns A-C by a number in column D. Column D's number is dependent on the data in Columns A-C (see below).



    https://docs.google.com/spreadsheets/d/11QSZPAl3nCs7nzpZIR4FzjfkPUyBBut1pD2K0919UB8/edit?usp=sharing



    I'm trying to make it so that there is a blank row up at the top (in A6:C6) so new data can be entered, then upon sorting it will move down into the main list (A7:C299) and a new blank row will appear in A6:C6 again.



    I tried doing this by moving the new entry to the bottom of the sheet, then having it sort, but for some reason it doesn't sort the new entry once it has been moved. If I delete the code for the sort, it moves correctly, and if I just have the code to sort (after it has copy-pasted the data from A6:C6), it also works, but when the functions are combined it doesn't. Any ideas or another way to accomplish this? Thanks!



    function SortList() {

    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    var range = sheet.getRange("A7:D299");

    // Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

    var copyrange = sheet.getRange("A6:C6");
    var pasterange = sheet.getRange("A298:C298");
    var copyvalues = copyrange.getValues();


    pasterange.setValues(copyvalues);
    copyrange.clearContent();

    // Sorts by the values in the first column (A)
    range.sort({column: 4, ascending: false});

    }









    share|improve this question

























      1












      1








      1








      I'm working with a google apps script to sort data in columns A-C by a number in column D. Column D's number is dependent on the data in Columns A-C (see below).



      https://docs.google.com/spreadsheets/d/11QSZPAl3nCs7nzpZIR4FzjfkPUyBBut1pD2K0919UB8/edit?usp=sharing



      I'm trying to make it so that there is a blank row up at the top (in A6:C6) so new data can be entered, then upon sorting it will move down into the main list (A7:C299) and a new blank row will appear in A6:C6 again.



      I tried doing this by moving the new entry to the bottom of the sheet, then having it sort, but for some reason it doesn't sort the new entry once it has been moved. If I delete the code for the sort, it moves correctly, and if I just have the code to sort (after it has copy-pasted the data from A6:C6), it also works, but when the functions are combined it doesn't. Any ideas or another way to accomplish this? Thanks!



      function SortList() {

      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var range = sheet.getRange("A7:D299");

      // Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

      var copyrange = sheet.getRange("A6:C6");
      var pasterange = sheet.getRange("A298:C298");
      var copyvalues = copyrange.getValues();


      pasterange.setValues(copyvalues);
      copyrange.clearContent();

      // Sorts by the values in the first column (A)
      range.sort({column: 4, ascending: false});

      }









      share|improve this question














      I'm working with a google apps script to sort data in columns A-C by a number in column D. Column D's number is dependent on the data in Columns A-C (see below).



      https://docs.google.com/spreadsheets/d/11QSZPAl3nCs7nzpZIR4FzjfkPUyBBut1pD2K0919UB8/edit?usp=sharing



      I'm trying to make it so that there is a blank row up at the top (in A6:C6) so new data can be entered, then upon sorting it will move down into the main list (A7:C299) and a new blank row will appear in A6:C6 again.



      I tried doing this by moving the new entry to the bottom of the sheet, then having it sort, but for some reason it doesn't sort the new entry once it has been moved. If I delete the code for the sort, it moves correctly, and if I just have the code to sort (after it has copy-pasted the data from A6:C6), it also works, but when the functions are combined it doesn't. Any ideas or another way to accomplish this? Thanks!



      function SortList() {

      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var range = sheet.getRange("A7:D299");

      // Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

      var copyrange = sheet.getRange("A6:C6");
      var pasterange = sheet.getRange("A298:C298");
      var copyvalues = copyrange.getValues();


      pasterange.setValues(copyvalues);
      copyrange.clearContent();

      // Sorts by the values in the first column (A)
      range.sort({column: 4, ascending: false});

      }






      sorting google-apps-script google-sheets






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 13:45









      NickNick

      82




      82
























          1 Answer
          1






          active

          oldest

          votes


















          5














          You can try adding the flush() method in between the two actions.




          flush()



          Applies all pending Spreadsheet changes.



          Spreadsheet operations are sometimes bundled together to improve
          performance, such as when doing multiple calls to Range.getValue().
          However, sometimes you may want to make sure that all pending changes
          are made right away, for instance to show users data as a script is
          executing.




          function SortList() {

          var sheet = SpreadsheetApp.getActiveSpreadsheet();
          var range = sheet.getRange("A7:D299");

          // Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

          var copyrange = sheet.getRange("A6:C6");
          var pasterange = sheet.getRange("A298:C298");
          var copyvalues = copyrange.getValues();

          pasterange.setValues(copyvalues);
          copyrange.clearContent();

          //Flush
          SpreadsheetApp.flush();

          // Sorts by the values in the first column (A)
          range.sort({column: 4, ascending: false});

          }





          share|improve this answer
























          • YOU ARE BRILLIANT I believe that did the trick. Much thanks.

            – Nick
            Nov 14 '18 at 12:30











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282414%2fgoogle-script-sort-troubleshooting-works-as-2-functions-but-not-combined%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          You can try adding the flush() method in between the two actions.




          flush()



          Applies all pending Spreadsheet changes.



          Spreadsheet operations are sometimes bundled together to improve
          performance, such as when doing multiple calls to Range.getValue().
          However, sometimes you may want to make sure that all pending changes
          are made right away, for instance to show users data as a script is
          executing.




          function SortList() {

          var sheet = SpreadsheetApp.getActiveSpreadsheet();
          var range = sheet.getRange("A7:D299");

          // Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

          var copyrange = sheet.getRange("A6:C6");
          var pasterange = sheet.getRange("A298:C298");
          var copyvalues = copyrange.getValues();

          pasterange.setValues(copyvalues);
          copyrange.clearContent();

          //Flush
          SpreadsheetApp.flush();

          // Sorts by the values in the first column (A)
          range.sort({column: 4, ascending: false});

          }





          share|improve this answer
























          • YOU ARE BRILLIANT I believe that did the trick. Much thanks.

            – Nick
            Nov 14 '18 at 12:30
















          5














          You can try adding the flush() method in between the two actions.




          flush()



          Applies all pending Spreadsheet changes.



          Spreadsheet operations are sometimes bundled together to improve
          performance, such as when doing multiple calls to Range.getValue().
          However, sometimes you may want to make sure that all pending changes
          are made right away, for instance to show users data as a script is
          executing.




          function SortList() {

          var sheet = SpreadsheetApp.getActiveSpreadsheet();
          var range = sheet.getRange("A7:D299");

          // Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

          var copyrange = sheet.getRange("A6:C6");
          var pasterange = sheet.getRange("A298:C298");
          var copyvalues = copyrange.getValues();

          pasterange.setValues(copyvalues);
          copyrange.clearContent();

          //Flush
          SpreadsheetApp.flush();

          // Sorts by the values in the first column (A)
          range.sort({column: 4, ascending: false});

          }





          share|improve this answer
























          • YOU ARE BRILLIANT I believe that did the trick. Much thanks.

            – Nick
            Nov 14 '18 at 12:30














          5












          5








          5







          You can try adding the flush() method in between the two actions.




          flush()



          Applies all pending Spreadsheet changes.



          Spreadsheet operations are sometimes bundled together to improve
          performance, such as when doing multiple calls to Range.getValue().
          However, sometimes you may want to make sure that all pending changes
          are made right away, for instance to show users data as a script is
          executing.




          function SortList() {

          var sheet = SpreadsheetApp.getActiveSpreadsheet();
          var range = sheet.getRange("A7:D299");

          // Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

          var copyrange = sheet.getRange("A6:C6");
          var pasterange = sheet.getRange("A298:C298");
          var copyvalues = copyrange.getValues();

          pasterange.setValues(copyvalues);
          copyrange.clearContent();

          //Flush
          SpreadsheetApp.flush();

          // Sorts by the values in the first column (A)
          range.sort({column: 4, ascending: false});

          }





          share|improve this answer













          You can try adding the flush() method in between the two actions.




          flush()



          Applies all pending Spreadsheet changes.



          Spreadsheet operations are sometimes bundled together to improve
          performance, such as when doing multiple calls to Range.getValue().
          However, sometimes you may want to make sure that all pending changes
          are made right away, for instance to show users data as a script is
          executing.




          function SortList() {

          var sheet = SpreadsheetApp.getActiveSpreadsheet();
          var range = sheet.getRange("A7:D299");

          // Takes values in first row, moves then to end of list and clears contents in first row, then sorts list

          var copyrange = sheet.getRange("A6:C6");
          var pasterange = sheet.getRange("A298:C298");
          var copyvalues = copyrange.getValues();

          pasterange.setValues(copyvalues);
          copyrange.clearContent();

          //Flush
          SpreadsheetApp.flush();

          // Sorts by the values in the first column (A)
          range.sort({column: 4, ascending: false});

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 8:36









          JacqueJacque

          3216




          3216













          • YOU ARE BRILLIANT I believe that did the trick. Much thanks.

            – Nick
            Nov 14 '18 at 12:30



















          • YOU ARE BRILLIANT I believe that did the trick. Much thanks.

            – Nick
            Nov 14 '18 at 12:30

















          YOU ARE BRILLIANT I believe that did the trick. Much thanks.

          – Nick
          Nov 14 '18 at 12:30





          YOU ARE BRILLIANT I believe that did the trick. Much thanks.

          – Nick
          Nov 14 '18 at 12:30


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282414%2fgoogle-script-sort-troubleshooting-works-as-2-functions-but-not-combined%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python