Append Data into Gdrive: Append Data to the top, skipping 1st row

Hello. I would like to append data to the top, skipping the 1st row as the row is the header. I tried to follow this link but I am unsure what optIndex is :

Below is my block code:

Below is my app script and my error reads: ReferenceError: index is not defined. No data is being inserted.

Hope to get help. Thanks.

Why not just use appendRow() to add the latest entry to the bottom, then sort the sheet on the timestamp? (using apps script)

@TIMAI2 I’m sorry, I don’t understand the part of the sort the sheet on the timestamp. There is only one sheet and the latest data, I would like it to be added to the 2nd row and the previous data will all be pushed one row down.

function doPost(e) {
var ss = SpreadsheetApp.openByUrl(<YOUR SHEET ID>); 
var sheet = ss.getSheetByName(<YOUR SHEET NAME>);
var ts = e.parameter.Time_Stamped;
var ref = e.parameter.Reference_Number;

//adds new data to last row

//sorts sheet data by Column A in descending order - newest timestamp first
sheet.sort({column: 1, ascending: false});

If your first row is frozen, this will not be included in the sorting

Hi Tim. Appreciate the help. However, the latest data is still being appended to the last row. The web component returns the exception per image below

line 14 in my appscript is the line

sheet.sort({column: 1, ascending: false});

The fact that you are getting html back indicates a problem with your script. Have you:

1 Open the script project
2 Go to Publish
3 Deploy as Web App
4 Project version: - select New from the dropdown
5 Execute the app as: your google account address (email)
6 Who has access to the app: Anyone, even anonymous
7 Press the Update button

You have to do this EVERY TIME you change your script

Noted about the html response. In my previous reply, I already did the steps that you mentioned. I then created a new app script, with the same code and run. Still the same response.

I then change the code to just append to last row and my response content is still in html, as per image below.

Hence, eventhough it runs as per what I tested (data append to last row), my script still has a problem? If yes, could you assist me in what could be the issue. Thank you.

Have you given the script permission to run? Try running the script in the code editor.