Original index from sheet after using Query functions from web app script

alright, I'll try this first, hopefully no errors :crossed_fingers: i'll get back to you once i've test run everything

If you already have a doGet(e) in your web app for that spreadsheet (if I remember correctly, this data was in a separate spreadsheet), you can include an if/if/else statement and add a defining parameter for each function. for example:

doGet(e)



if (e.parameter.func == getDate ) {

//run function
getRowsFromDates(e.parameter.date);

} else if (e.parameter.func == getName ) {

// run function
getRowsFromNames(e.parameter.name);
}

blocks (9)

nope, my get name is in doPost instead of doGet so I think i'm good maybe? unless an error comes up

That is what you said yesterday :slight_smile: and it doesn't make sense. You are either making a GET request or a POST request.

Some errors showed up...
image

Here is the block:

I added some data in my spreadshit with the exact same date to see how it handles more than just one data with the same date. It does not show it though? I'm not sure how to sperate them. I tried to put it as a list then use for each item in the list thing but it doesnt work, just errors

The web app is returning a list of lists, you will have to iterate over each item in the list of lists to return all the data (using my example data):

returns

image

I swear I tried this earlier but maybe not im not even sure anymore, so many sleepless nights :joy:

I have another question. So I have one part where for example we edit a resident's name from the elderly's sheet, I want it to also update the names in both the medicine sheet and appointment sheet.

I have a rough idea of how it should be but I just wanted to confirm whether the code in the app script would be right for it, here is my script as for now for both the medicine and appointment sheets:

Medicine:

function doGet(e) {
  return ManageSheet(e);
}
function doPost(e){
  return ManageSheet(e);
}

function ManageSheet(e){
  //Read All Records
  if( e.parameter.func == "ReadAll"){
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    var rg = sh.getDataRange().getValues();
    var outString = '';
    for(var row=0; row<rg.length; ++row){
      outString += rg[row].join(',')+'\n';
    } 
    return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
  }
 
  
  //Delete Single
  else if (e.parameter.func=="Delete"){
    var record = e.parameter.id;
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    sh.deleteRow(parseInt(record)+1);
    return ContentService.createTextOutput("Success, requested action completed");
  }
  
  //Read Single Record
  else if(e.parameter.func == "ReadRecord"){
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    var rg = sh.getDataRange().getValues();
    var outString = '';
    outString += rg[parseInt(e.parameter.id)].join(',');
    return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
  }
  
  //Update Single Record
  else if(e.parameter.func == "Update"){
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    var data = [[e.parameter.medicine, e.parameter.time]];
    sh.getRange("B"+(parseInt(e.parameter.id)+1)+":C"+(parseInt(e.parameter.id)+1)).setValues(data);
    return ContentService.createTextOutput("Success");
  }
  
  //Create new record
  if(e.parameter.func == "Create"){
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    var data = [e.parameter.name,e.parameter.medicine, e.parameter.time];
    sh.appendRow(data);
    return ContentService.createTextOutput("Success");
  }
}

Appointment:

function doGet(e) {
  return ManageSheet(e);
}
function doPost(e){
  return ManageSheet(e);
}

function ManageSheet(e){
  //Read All Records
  if( e.parameter.func == "ReadAll"){
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    var rg = sh.getDataRange().getValues();
    var outString = '';
    for(var row=0; row<rg.length; ++row){
      outString += rg[row].join(',')+'\n';
    } 
    return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
  }
  
  //Delete Single
  else if (e.parameter.func=="Delete"){
    var record = e.parameter.id;
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    sh.deleteRow(parseInt(record)+1);
    return ContentService.createTextOutput("Success, requested action completed");
  } 
  
  //Read Single Record
  else if(e.parameter.func == "ReadRecord"){
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    var rg = sh.getDataRange().getValues();
    var outString = '';
    outString += rg[parseInt(e.parameter.id)].join(',');
    return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
  }
  
  //Update Single Record
  else if(e.parameter.func == "Update"){
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    var data = [[e.parameter.name,e.parameter.date, e.parameter.time, e.parameter.hospital]];
    sh.getRange("A"+(parseInt(e.parameter.id)+1)+":D"+(parseInt(e.parameter.id)+1)).setValues(data);
    return ContentService.createTextOutput("Success");
  }
  
  //Create new record
  if(e.parameter.func == "Create"){
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    var data = [e.parameter.name,e.parameter.date, e.parameter.time, e.parameter.hospital];
    sh.appendRow(data);
    return ContentService.createTextOutput("Success");
  }
}

Well both are actually identical only some parameters are different.

So lets say I want to update the names do I change the update part to ??:

else if(e.parameter.func == "UpdateName"){
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheets()[0];
    var data = [[e.parameter.name]];
    sh.getRange("A"+(parseInt(e.parameter.id1)+1)).setValues(data);
    return ContentService.createTextOutput("Success");
  }

Buuuutt then thinking about it, that wouldn't work because it needs the indexes of both the medicine and appointment sheet of where the resident's name is and their names are repeated multiple times in those lists to update the name. Maybe a query function is a better approch then but how to update it using script again? can you help please, I believe it'll be more or less just like the delete query you helped me with before but for update

Hmmm if I remember correctly, your data for medicine and appointments does not have a specific id column, so the "update" routine will not work as it stands (This problem was caused by the person who copied my code and made the youtube video you followed without thinking it through! :rage:). In which case you will have to get the index of the item in the medicine or appointment list, and use that to compare against the "row" in the sheet.

Can you post an image of your medicine or appointment sheet, and I will have a look at the scripting...

For Appointment - something like this:

else if(e.parameter.func == "Update"){

var data = [[e.parameter.name,e.parameter.date, e.parameter.time, e.parameter.hospital]];
var index = e.parameter.index;

  for ( var row = 1; row<rg.length;row++ ) {
    if ( row == index ) {
      sh.getRange(row+1,1,1,4).setValues(data);    
    }

return ContentService.createTextOutput("Update Successful");

}

You need to send the correct list index in the app as a parameter - index.

Check what happens with the date and time if google sheets converts them to dates and times, you may need to escape an apostrophe. For example, instead of sending this: "05/11/2020" you may need to send this: "\'05/11/2020"

Yeah, my data does not have a specific id. I'm sorry to hear about that, I didn't know you are the original author that I followed the code for in the video... I will add you as a reference to most of my codes in my technical report because of all the super duper help you've given me :smiling_face_with_three_hearts:

image
image

I was thinking of making two seperate update functions where one is the normal updating function and the other is a query update function where it updates the resident's name in both the medicine and appointment sheets. Using a query like: UPDATE Sheet1 SET A = "Name" WHERE A = "Original Name" the original name i'll extract it using a tiny db do you think that is possible to do because i'm not quite sure?

I am not sure either because you have multiple instances if the same name in both, and the indexes and the data do not align.....