I need some help with Apps Script and Google Sheets. I don't know much about coding and programming, but I'm trying to tweak a script. I have a website that sends variables to Apps Script and then posted in Google Sheets. But I want these variables in two separate groups. Let's call them Data 1 and Data 2. Data 1 goes in Sheet 1, and Data 2 goes in Sheet 2. Currently, both data are sent and processed under one function, meaning Sheet 1 and Sheet 2 are populated at the same time. Also both data include a timestamp per row. However, if there is no Data 2, Sheet 2 is still being populated with timestamps. But I only want either Sheet 1 or Sheet 2 (no timestamps or anything else in the other sheet) depending on what the website sends.
I tried to split the script into two functions, but only one ended up working, perhaps because I'm using the same function name? I tried to rename the second function, but it didn't work either, possibly because I may be breaking naming conventions or the way App Script doesn't allow two postData per script? If empty variables are not a valid factor for Apps Script to stop populating the other sheet, do I need to use conditionals, and if yes, how? I'm not sure where to go. Could you help me with this? Here's a script:
function doPost(postData) {
let doc = SpreadsheetApp.openById(MY_SHEET_ID);
let sheet = doc.getSheetByName("Sheet 1");
let sheet2 = doc.getSheetByName("Sheet 2");
let parsedData = postData.parameter;
parsedData = JSON.parse(Object.keys(parsedData));
let userBlue = parsedData["Blue"]
let userPurple = parsedData["Purple"]
let userRed = parsedData["Red"]
let userOrange = parsedData["Orange"]
sheet.appendRow([new Date(), userBlue, userPurple]);
sheet2.appendRow([new Date(), userRed, userOrange]); return ContentService.createTextOutput("Success");
}
Thank you in advance