简体   繁体   中英

Google Sheets / Form Insert New Row at the Top

I have a problem with Google Sheets. I recently created a Google Form that allows people to leave feedbacks however when people fill out the form I do not want the new row on the google sheet to be inserted below I want the row to be inserted at the top.

I've tried using this code below however it does not work.

function rtoFeedbacks() {

  var spreadsheet = SpreadsheetApp.openById("1bXqkYxQKvPg3urAdsadxdsaDW8KvwEOa9P47PpE");
  var sheet = spreadsheet.getSheetByName("Feedback Form");
  sheet.insertRowsBefore(2,1);
}

New row still insert a the bottom

Yeah, so if that is the case of having timestamps, using:

range.sort({column: 1, ascending: false}); //or ascending: true

if there is a header row that you don't want included in the sort so it stays up top use:

setFrozenRows(1)

https://developers.google.com/apps-script/reference/spreadsheet/range#sortsortspecobj https://developers.google.com/apps-script/reference/spreadsheet/sheet#setfrozenrowsrows

I tried this out and it works for me:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.setFrozenRows(1)
  var range = sheet.getDataRange();
  range.sort({column: 1, ascending: false});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM