0

I have a server code to connect to a page that staff use for entering new customers details. The page also inherited from Google Drive Tables with same fields as a page and sheet. Unfortunately, I don't know how to connect my server code to my page so when new data entered and submit button pushed the NewCustomer() function gets called and insert that data into a new row at the end of my spreadsheet. I just start using appmaker last week and I am sorry if my question is very babyish. Any help really appreciated.

server code:

function NewCustomer() {
    var spreadSheet = SpreadsheetApp.openById("***").getActiveSheet()[0],
        dataToBackUp = [],
        globalKeys = {
          model: ["Channel", "Owner", "INQDate","CustomerName", "CNT", "Contact", "Email",
                  "Amount", "Status", "TargetDate", "Type", "Transaction", "NoteUpdate"],
          label: ["Channel", "Owner", "INQ Date","Customer Name", "CNT", "Contact", "Email",
                  "Amount", "Status", "Target Date", "Type", "Transaction", "Note Update"],
        };
    var records = app.models.requests.newQuery().run();
    if(records.length >= 1) {
      for (var i = 0; i < records.length; i++) {
        var newLine = [];
        for (var x = 0; x < globalKeys.model.length; x++) {
          newLine.push(records[i][globalKeys.model[x]]);
        }
        dataToBackUp.push(newLine);
        if(i === records.length - 1) {
          if(dataToBackUp.length >= 1) {  
               spreadSheet.appendRow(dataToBackUp);

          }
        }
      }
    }
  }
Maddi
  • 13
  • 5

1 Answers1

0

So, if your only problem is communication between client and server, then you can do it using this snippet:

// onClick button event handler
google.script.run
   .withFailureHandler(function(error) {
      // TODO: An error occurred, so display an error message.
    })
  .withSuccessHandler(function(result) {
     // TODO: Handle success
   })
 .myPublicServerSideFunction(param1, param2);

You can read more about client-sever communication by following the links bellow:

https://developers.google.com/apps-script/guides/html/reference/run https://developers.google.com/appmaker/scripting/client#call_a_server_script

If you want to make your data backup, then you can just go to Settings -> Deployments, select your deployment and Export Data. If you want to automate the process then just switch to Cloud SQL and it will take care about backups for you.

Pavel Shkleinik
  • 6,298
  • 2
  • 24
  • 36