最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google apps script - Unable to create Appscript Trigger Again. Error: Something went wrong. Please reload the page to try again

programmeradmin1浏览0评论

I created a new project and ran the following script. It has deleted my previously existing triggers and even after removing

function createTrigger() {
  // Delete all existing triggers to avoid duplicates
  var triggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }

  // Create a time-driven trigger to run the function every Friday at a specific time (e.g., 9:00 AM)
  ScriptApp.newTrigger('sendWeeklyEmail')
    .timeBased()
    .onWeekDay(ScriptApp.WeekDay.FRIDAY)
    .atHour(9) // Change the hour as needed
    .create();
}

It is still not allowing to add any new triggers.

The error says Loading Data. This may take a few moments. and then Something Went Wrong. Please reload the page to try again

function sendWeeklyEmail() {
  var today = new Date();
  var dayOfWeek = today.getDay();
  
  // Check if today is Friday (day 5)
  if (dayOfWeek !== 5) {
    return; // Exit if it's not Friday
  }

  var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);
  var lastFriday = lastDayOfMonth.getDate() - lastDayOfMonth.getDay() + 5;
  
  // Check if today is the last Friday of the month
  if (today.getDate() === lastFriday) {
    return; // Don't send email on the last Friday of the month
  }

  // Format the current month and year as "MMM-YYYY" (e.g., "Mar-2025")
  var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  var sheetName = monthNames[today.getMonth()] + '-' + today.getFullYear();

  // Get the Google Sheet data from the current month sheet
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); 
  
  // If the sheet doesn't exist, return
  if (!sheet) {
    Logger.log("Sheet for " + sheetName + " not found.");
    return;
  }

  // Assuming the defaulter data is in column A starting from row 2
  var dataRange = sheet.getRange('A2:A'); // Adjust the range if needed
  var values = dataRange.getValues();
  
  // Calculate the sum of the defaulters' count
  var sum = 0;
  for (var i = 0; i < values.length; i++) {
    sum += values[i][0] || 0;
  }

  // Define email content
  var emailSubject = 'Weekly Defaulter Count for ' + sheetName;
  var emailBody = 'Dear Manager,\n\nThe count of defaulters for this week is: ' + sum + '.\n\nPlease review the Google Sheet report to take further action.\n\nBest regards,\nYour Name';

  // Set email recipient (Manager) and CC (HR)
  var recipient = '[email protected]'; // Change this to your manager's email
  var ccEmail = '[email protected]'; // Change this to HR's email address

  // Send the email with HR in CC
  MailApp.sendEmail({
    to: recipient,
    cc: ccEmail,
    subject: emailSubject,
    body: emailBody
  });
}

function createTrigger() {
  // Delete all existing triggers to avoid duplicates
  var triggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }

  // Create a time-driven trigger to run the function every Friday at a specific time (e.g., 9:00 AM)
  ScriptApp.newTrigger('sendWeeklyEmail')
    .timeBased()
    .onWeekDay(ScriptApp.WeekDay.FRIDAY)
    .atHour(9) // Change the hour as needed
    .create();
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论