dwyl / learn-to-send-email-via-google-script-html-no-server

:email: An Example of using an HTML form (e.g: "Contact Us" on a website) to send Email without a Backend Server (using a Google Script) perfect for static websites that need to collect data.
GNU General Public License v2.0
3.15k stars 910 forks source link

Adding new entries to the top of google sheet #423

Closed jmorim closed 2 years ago

jmorim commented 2 years ago

I don't know if someone has an easy way of doing this but I imagine it involves messing with the gscript and I don't know how to debug those to get this working quickly. I just want new entries to be at the top of the sheet so I don't have to scroll down for new data. If no one has a quick solution I'll report back if I figure it out.

mckennapsean commented 2 years ago

getRange returns an API specified here: https://developers.google.com/apps-script/reference/spreadsheet/range

You can probably use insertCells first to create a new row (given the number of columns), then add the data at the newly inserted row. The pointers would need to be updated to be the second row (right after the header), but should work. Alternatively, there may be a sorting option to sort the data for you. Or you could create a kind of pivot table that auto sorts the data based on time for you too.

bledatunay commented 2 years ago

If you don't want to mess with the script: On the sheets interface, you can right-click on the letter of your "Timestamp" column (default of the script: A) and select "Sort Sheet Z to A" from the dropdown - though when the form receives more messages, they would again be added to the bottom, so you would need to do this every time you want newer messages to be at the top.

jmorim commented 2 years ago

Replaced

var nextRow = sheet.getLastRow() + 1;
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);

with

var newRow = sheet.getRange(2, 1, 1, newHeader.length);
newRow.insertCells(SpreadsheetApp.Dimension.Rows);
newRow.setValues([row]);

and got it working