Create record in google sheet

Hi, I'm having trouble creating a record in Google Sheets.
Deleting, updating, and listing work, but I can't create a record.

> function doGet(e) {
> 
>     var ss = SpreadsheetApp.openById(e.parameter.ID); // otwórz skoroszyt
>     var sh = ss.getSheetByName(e.parameter.SH);
>     var rg = sh.getDataRange().getValues();
> 
>   // CZYTAJ DANE
>   if (e.parameter.func == "CZYTAJ") {
>     var data = sh.getDataRange().getValues();
>     return ContentService.createTextOutput(JSON.stringify(data))
>           .setMimeType(ContentService.MimeType.JSON);
>   }
>   // USUN OSOBE
>   else if (e.parameter.func == "USUN") {
>     var record = e.parameter.id;
>     sh.deleteRow(parseInt(record) + 1);
>     return ContentService.createTextOutput("USUNIĘTO");
>   }
>   //CZYTAJ ZAPIS
>   else if ( e.parameter.func == "POJEDYNCZY") {
>     var ss = SpreadsheetApp.openById(e.parameter.ID); // otwórz skoroszyt
>     var sh = ss.getSheetByName(e.parameter.SH);
>     var rg = sh.getDataRange().getValues();
>     var outString = '';
>     outString += rg[parseInt(e.parameter.id)].join(',');
>     return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
>   } 
>   // ZMIEN OSOBE
>   else if (e.parameter.func == "ZMIEN") {
>     var data = [
> 	[
> 	e.parameter.jeden,
> 	e.parameter.dwa,
> 	e.parameter.trzy,
> 	e.parameter.cztery,
> 	e.parameter.piec,
> 	]
>     ];
>     sh.getRange("B" + (parseInt(e.parameter.id) + 1) + ":F" + (parseInt(e.parameter.id) + 1 )).setValues(data);
>     return ContentService.createTextOutput("ZMIENIONO");
> 
>   }
>   //dodoaj wpis
>   else if (e.parameter.func == "STWORZ") {
>     var ss = SpreadsheetApp.getActive();
>     var sh = ss. getSheets()[0];
>     var data =[[e.parameter.id, e.parameter.jeden. e.parameter.dwa, e.parameter.trzy, e.parameter.cztery, e.parameter.piec]];
>     sh. appendRow(data);
> 
> return ContentService.createTextOutput ("Success" );
> }
> }
![record |690x302](upload://onAnOIBf4Pnt0PVglAiZYzH8D7P.png)

![record |690x302](upload://onAnOIBf4Pnt0PVglAiZYzH8D7P.png)

In all other functions you are calling perfectly the ss with param id but in the create alome you are using get active sheet. Why dont you use there also the sheet id?
May be try this code

else if (e.parameter.func == "STWORZ") {
  var ss = SpreadsheetApp.openById(e.parameter.ID); // Open by ID
  var sh = ss.getSheetByName(e.parameter.SH); // Or use getSheets()[0] if appropriate
  var data = [e.parameter.id, e.parameter.jeden, e.parameter.dwa, e.parameter.trzy, e.parameter.cztery, e.parameter.piec];
  sh.appendRow(data);
  return ContentService.createTextOutput("Success");
}

Also add error alert to know actual error message to know your problem also i have changed the structure of your data too. Append function will expects data as single array. So test the above code and ket us know whether it is working or not

1 Like

Thank you, it worked.

One more problem: the buttons scroll with the screen. If the list is too long, is there a way to keep them in one position?

Glad the code works.

I believe you are using listview, set fixed height. When user scrolls the list it keeps the buttons in the same place
(As default the listview height was set with automatic height)



Hi, I have another problem... If the ID is on the list, I don't want it to create it in the sheet, but to throw away the information about its existence and entering a non-existent ID.

Why do you want to create another record when you delete a record?

No, I want to create another record, but if there is already one on the sheet, it should not create another one, but display a message that one already exists

In such case before you save the record get the gsheet data and check whether the new data is in this ghseet data list using the block isin the list.

If exists
Then through alert
Else proceed to save the save the data

You might have to redesign the way you store your data to resemble a bank transaction register, that only accepts new increment rows, and is set up to show balances from a separate summary sheet.

Search this board for my Marathon sample

Which is it ?