2021-12-20

Can you delete a specific sheet without knowing sheet ID?

I have a code which does the following:

  1. Collects values from first sheet "OTW Sheet"
  2. Opens another existing sheet by Id - "Invoice"
  3. Sets values in "Invoice" as copied from "OTW Sheet"
  4. Creates a new spreadsheet in the Drive App and copies "Invoice" to new Spreadsheet.

This all works well but the next step is to delete the sheet called "Sheet1" in the newly created Spreadsheet. This part i can't get to work. No errors appear, it just fails to delete the sheet.

Code is as follows:

function myFunction() {
  
  var OrderSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OTW Sheet");
  var Row = OrderSheet.getLastRow();
  var Values1 = OrderSheet.getRange(Row, 5).getValue();
  var Cost1 = OrderSheet.getRange(1, 16).getValue();

  var Invoice = SpreadsheetApp.openById("19wv72wCNsPV6_hK8XCRcMBkfzEEU1IrJB6scuPz2Vfo"); 
  Invoice.getSheetByName("BLANK");
  
  var Newsheet = Invoice.getActiveSheet();
  Newsheet.getRange(17, 7).setValue(Cost1);
  Newsheet.getRange(1, 15).setValue(Values1);

  var NewSS = SpreadsheetApp.create('OTW Invoice'); 
  Newsheet.copyTo(NewSS).setName('OTW Invoice ' + Cost1).getSheetId();
  var Folder = DriveApp.getFoldersByName("TEST Invoices").next();
  var CopyFile = DriveApp.getFileById(NewSS.getId());
  Folder.addFile(CopyFile);

  NewSS.getId();
  SpreadsheetApp.openById(NewSS).getSheetByName("Sheet1").deleteSheet();
  
}```


from Recent Questions - Stack Overflow https://ift.tt/3J3eb25
https://ift.tt/eA8V8J

No comments:

Post a Comment