Google Apps script. For loop is not working after Duplicating the workbook

  Kiến thức lập trình

New to AppScripting. In the script below I have a spreadsheet that works but I created a button that copies the template workbook into a new spreadsheet in folder of google drive. The problem is with a different button that creats a new sheet in the workbook. The for loop doesn’t seem to be executing and it just tries to create a new sheet. The for loop is supposed to display a message if the sheet already exists.

function newSheet() {

const sourceWbId = '1AIauqZi0SQkKPlFqSbCyXinUep7bjCspEJnl5_ic6y4';
let wb = opener(sourceWbId);
let sheets = wb.getSheets();
   
  let source = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = source.getSheetByName('Template');

  //var summary = source.getSheetByName('Shipping Summary');

  const date = new Date();
  //or use
  const dateDisplay = new Date().toLocaleDateString();

  const year = date.getFullYear();
  const month = date.getMonth()+1;
  const day = date.getDate();
  let today = `${month}/${day}/${year}`;

const sheetIgnore = [today];

let ui = SpreadsheetApp.getUi();
`

  for(i in sheets) {
   if(sheets[i].getSheetName() == today) {
      let buttonPushed = ui.alert('Error','This sheet aready exists. ' + dateDisplay,ui.ButtonSet.OK_CANCEL);
      if(buttonPushed == 'OK'){
        source.toast('Accepted');
        return;
      }
      else if(buttonPushed == 'CANCEL'){
        source.toast('Canceled Operation');
        return;
      }
   }
   Logger.log(sheets)
  }
    if(sheets !== today){
      sheet.copyTo(source).setName(today);
      let targetSheet = source.getSheetByName(sheetIgnore);
      targetSheet.getRange(1,6).setValue('Date:' + String.fromCharCode(10) + today);
      targetSheet.getRange(1,5).setValue(false);
      targetSheet.getRange(1,4).setValue('Completed Send Email');
    }
   
  

}

New contributor

Paul Edmark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT