function main() {
  
  //Defining the variables
  var SPREADSHEET_URL = "[SPREADSHEET URL WITH DATA]";
  var email="[EMAIL]";
  var ccemail="[EMAIL]";
  var scriptName="All Accounts Replacer";
  var PCsheet = 'Desktop SL';
  var Mobilesheet = 'Mobile SL';
  var accountsToCheck = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getSheetByName(PCsheet).getRange('H1').getValue().split(','); //get the accounts to run on
  
  //MCC - select the account to work on
  var accountSelector  = MccApp.accounts().withIds(accountsToCheck);
  var accountIterator = accountSelector.get();
  
  //iteration on the accounts
  while (accountIterator.hasNext()) {
      var account = accountIterator.next();
      MccApp.select(account);
      var accountName = account.getName();
      Logger.log("accountName" + accountName);
  
      //Setting Values in Spreadsheet
      //spreadsheet.getRangeByName("date").setValue(new Date());
      //spreadsheet.getRangeByName("account_id").setValue(AdWordsApp.currentAccount().getCustomerId());
      
        // ***********PC Sitelinks
      var dataspreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getSheetByName(PCsheet);
      var SpeadsheetDataArray = dataspreadsheet.getRange(3, 1, dataspreadsheet.getLastRow() - 2, dataspreadsheet.getLastColumn()).getValues();
      
       // SpeadsheetDataArray.forEach(myFunction);
      
      function myFunction(SpeadsheetDataArray){
        var condition = (SpeadsheetDataArray[5]);
        if ( condition == "yes"){
          Logger.log(condition);
          Logger.log(SpeadsheetDataArray[6]);
          
          //This is the way to select a specific SL in order to modify
          var sitelinkSelector = AdWordsApp.extensions()
          .sitelinks()
          .withIds([SpeadsheetDataArray[6]]);
          
          //now start replacing SLs
          var oldSLUrl= SpeadsheetDataArray[7];
          var newSLUrl= SpeadsheetDataArray[8];
          var sitelinkIterator = sitelinkSelector.get();
          
          while (sitelinkIterator.hasNext()) {
            var sitelink = sitelinkIterator.next();
            var sitelinkOld=sitelink.urls().getFinalUrl();
            var sitelinkNew=sitelinkOld.replace(oldSLUrl, newSLUrl);
            
            //Logger.log("zz" + sitelink.getLinkText()+" "+sitelink.getLinkUrl());
            
            sitelink.setLinkText(SpeadsheetDataArray[1]);
            sitelink.setDescription1(SpeadsheetDataArray[2]);
            sitelink.setDescription2(SpeadsheetDataArray[3]);
            sitelink.urls().setFinalUrl(sitelinkNew);
            
            Logger.log("The following SL is now running: " + sitelink.getLinkText() + sitelink.getDescription1() +"\n" + sitelink.getDescription2() +"\nThe Url changed from "
            + sitelinkOld + " to " + sitelink.urls().getFinalUrl());
            
            //Logger.log(IDs);
            
            //Sending Email telling about the change
            MailApp.sendEmail({
              to:email,
              cc:ccemail,
              subject:"Sitelinks in AdWords Account " + accountName + " (" + AdWordsApp.currentAccount().getCustomerId() + ") were changed", 
              htmlBody: scriptName + " Script ran and The following PC SL ("+ sitelink.getLinkText()+ ") is now running on account " + accountName
              + "\n\nLog into AdWords and take a look.\n\nAlerts dashboard: " + SPREADSHEET_URL
            });
            
          }
          
        }
        
      } //end myFunction
        SpeadsheetDataArray.forEach(myFunction);
    
      
      //******MOBILE   Sitelinks
      var dataspreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getSheetByName(Mobilesheet)
      var SpeadsheetDataArray = dataspreadsheet.getRange(3, 1, dataspreadsheet.getLastRow() - 2, dataspreadsheet.getLastColumn()).getValues();
      //SpeadsheetDataArray.forEach(myFunction);
      
      function myFunction(SpeadsheetDataArray){
        var condition = (SpeadsheetDataArray[5]);
        if ( condition == "yes"){
          Logger.log(condition);
          Logger.log(SpeadsheetDataArray[6]);
          
          //This is the way to select a specific SL in order to modify
          var sitelinkSelector = AdWordsApp.extensions()
          .sitelinks()
          .withIds([SpeadsheetDataArray[6]]);
          
          //now start replacing SLs
          var oldSLUrl= SpeadsheetDataArray[7];
          var newSLUrl= SpeadsheetDataArray[8];
          var sitelinkIterator = sitelinkSelector.get();
          
          while (sitelinkIterator.hasNext()) {
            var sitelink = sitelinkIterator.next();
            var sitelinkOld=sitelink.urls().getFinalUrl();
            var sitelinkNew=sitelinkOld.replace(oldSLUrl, newSLUrl);
            
            // Logger.log("zz" + sitelink.getLinkText()+" "+sitelink.getLinkUrl());
            
            sitelink.setLinkText(SpeadsheetDataArray[1]);
            sitelink.setDescription1(SpeadsheetDataArray[2]);
            sitelink.setDescription2(SpeadsheetDataArray[3]);
            sitelink.urls().setFinalUrl(sitelinkNew);
            
            Logger.log("The following SL is now running: " + sitelink.getLinkText() + sitelink.getDescription1()  +"\n" + sitelink.getDescription2() +"\nThe Url changed from "
            + sitelinkOld + " to " + sitelink.urls().getFinalUrl());
            
            //Logger.log(IDs);
            
            //Sending Email telling about the change
            MailApp.sendEmail({
              to:email,
              cc:ccemail,
              subject:"Sitelinks in AdWords Account " + accountName + " (" + AdWordsApp.currentAccount().getCustomerId() + ") were changed", 
              htmlBody:scriptName + " Script ran and The following Mobile SL ("+ sitelink.getLinkText()+ ") is now running on account " + accountName
              + "\n\nLog into AdWords and take a look.\n\nAlerts dashboard: " + SPREADSHEET_URL
            });
            
          }
          
        }
        
      } //end myFunction
          SpeadsheetDataArray.forEach(myFunction);

  }//end of while accounts loop
}