Reputation: 31
I have a workbook with lots of sheets, each titled as someone's last name. Ex. Jones, Smith, and Williams each have a sheet.
I've written a script, and based on a prompt where I put in the person's name when I run the script, I would like to delete their sheet in my workbook. With the current script I have, I have an error when I run.
Any help?
function DeleteEmployee() {
//Enter employee's name
var Sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Daily Overview")
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('First Name', 'Please enter the first name of the employee', ui.ButtonSet.OK_CANCEL);
var FirstName = response.getResponseText();
var ui2 = SpreadsheetApp.getUi();
var response2 = ui2.prompt('Last Name', 'Please enter the last name of the employee', ui.ButtonSet.OK_CANCEL);
var LastName = response2.getResponseText();
//Deletes the employee's sheet
var Source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(LastName).deleteActiveSheet();
}
Upvotes: 0
Views: 141
Reputation: 64140
function DeleteEmployee() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("Daily Overview")
var ui=SpreadsheetApp.getUi();
var response=ui.prompt('First Name', 'Please enter the first name of the employee', ui.ButtonSet.OK);
var FirstName=response.getResponseText();
response=ui.prompt('Last Name', 'Please enter the last name of the employee', ui.ButtonSet.OK);
var LastName=response.getResponseText();
var sheet=ss.getSheetByName(LastName);
ss.deleteSheet(sheet)
}
Upvotes: 0
Reputation: 8077
1) You try to find the sheet with only the last name; I would assume with employee records, their first name is also there? Assuming sheets are named in "LastName, FirstName" format, you can use/adjust the following:
var Source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(LastName + ", " + FirstName);
2) The documentation for deleteSheet()
shows that it's a function of the Speadsheet
object, not the Sheet
object, and you'd need to pass in the sheet object. So you would have to call it like this:
SpreadsheetApp.getActiveSpreadsheet().deleteSheet(Source);
Upvotes: 1