/**
 * atulvij.com - Google Workspace course, Lesson 6.
 * Paste into Extensions > Apps Script of the practice sheet, save, then Run > dailySummary.
 * The first run asks for permission: review what it needs (this sheet + send email as you).
 */
function dailySummary() {
  const sh = SpreadsheetApp.getActive().getSheetByName('Orders');
  const rows = sh.getDataRange().getValues().slice(1);          // skip header
  const byRegion = {};
  rows.forEach(r => { byRegion[r[2]] = (byRegion[r[2]] || 0) + Number(r[6]); });
  const lines = Object.keys(byRegion).sort()
    .map(k => k + ': Rs ' + byRegion[k].toLocaleString('en-IN'));
  const total = rows.reduce((s, r) => s + Number(r[6]), 0);
  const body = 'Sales by region

' + lines.join('
') + '

Total: Rs ' + total.toLocaleString('en-IN');
  // Sends to YOURSELF only. Change the address only to people who expect it.
  MailApp.sendEmail(Session.getActiveUser().getEmail(), 'Daily sales summary', body);
  SpreadsheetApp.getActive().toast('Summary sent to you');
}

/** Adds a custom menu so non-technical users can run it with a click. */
function onOpen() {
  SpreadsheetApp.getUi().createMenu('Reports').addItem('Email me the summary', 'dailySummary').addToUi();
}
