0

I have this html application which will look for a certain ID number then it will populate the field in the userform. Once I click the button Update Data, I am hoping that it will update the specific row values in the google sheet, but I always end up getting Uncaught at updateProfile (fcn:81). "(fcn:81)" end up to be var newDataRow = ws.getRange(myPnum + 1, 1, 1, ws.getLastColumn()).setValues(...

Here is my javascript code:

document.getElementById("updatebtn").addEventListener("click", updateAppProfile);

//----------------------  UPDATE PROFILE  ---------------------------------//   
   function updateAppProfile(){

     var upProfile = document.getElementById("profileNum").value;

     if(upProfile.length === 6){
        var myNewData = {};

        myNewData.profnum = document.getElementById("profileNum").value;
        myNewData.firstName = document.getElementById("fname").value;
        myNewData.middleName = document.getElementById("mname").value;
        myNewData.lastName = document.getElementById("lname").value;
        myNewData.appsex = document.getElementById("sex").value;
        myNewData.civStat = document.getElementById("cstatus").value;
        myNewData.citizen = document.getElementById("citizenship").value;
        myNewData.birthdate = document.getElementById("bdate").value;


    //  console.log(myNewData);
    //  alert("OK");
        google.script.run.updateMyProfile(upProfile,myNewData);

     } 
     else{
     alert("invalid Profile Number");
     }   
   }

    </script>

Here is my google app script function:

function updateMyProfile(upProfile,myNewData){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ws = ss.getSheetByName("Results");

  var data = ws.getRange(1, 1, ws.getLastRow(), 1).getValues();
  var dJoin = data.join().split(",");
  var myPnum = dJoin.indexOf(upProfile);

  if (myPnum > -1){
    var newDataRow = ws.getRange(myPnum + 1, 1, 1, ws.getLastColumn()).setValues([[myNewData.profnum,
                 myNewData.firstName,
                 myNewData.middleName,
                 myNewData.lastName,
                 myNewData.appsex,
                 myNewData.civStat,
                 myNewData.citizen,
                 myNewData.birthdate
                 ]]);
  } else {
    Logger.log("unknown data")
  }

}

I don't know what to do since when I test the data on the google script side only it work. But when I used the whole program it ends up in that error. I hope anyone can help. Thank you in advance. I will really appreciate your answers. thank you.

1 Answer 1

1

Try wrapping your function in a try catch block in order to debug and find the precise error.

  • [Try / Catch][1]

Besides that:

var newDataRow = = ws.getRange(myPnum + 1, 1, 1, ws.getLastColumn()) is not as flexible as actually using values from the array you are placing into the row. So rather use the following syntax:

var outputRowArray = [ myNewData.firstName, myNewData.middleName, "etc…" ] and then use its length to determine the range like so:

var newDataRow = ws.getRange( myPnum + 1, 1, 1, outputRowArray.length )

And you currently have a Array inside of an Array inside of setValues(), but since you are only adding 1 row, 1 array should be used, ie 1 row = 1 array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.