समझिए
Range की formatting
function formatReport() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sales');
const lastRow = sheet.getLastRow();
// Header row
sheet.getRange('A1:E1')
.setFontWeight('bold')
.setBackground('#1f4e78')
.setFontColor('#ffffff')
.setHorizontalAlignment('center');
// Number and date formats
sheet.getRange(2, 1, lastRow - 1, 1).setNumberFormat('dd-mmm-yyyy');
sheet.getRange(2, 4, lastRow - 1, 2).setNumberFormat('₹#,##0.00');
// Borders, freeze, auto-size
sheet.getRange(1, 1, lastRow, 5).setBorder(true, true, true, true, true, true);
sheet.setFrozenRows(1);
sheet.autoResizeColumns(1, 5);
}
ज़्यादातर setters range ही लौटाते हैं, इसलिए ऊपर की तरह इन्हें chain कर सकते हैं। लाख वाली grouping (1,23,456.00) के लिए File → Settings में spreadsheet का locale India रखें — Sheets अंकों की grouping locale से करता है।
अपना menu
onOpen हर बार spreadsheet खुलने पर अपने-आप चलता है:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('ExcelWalaa Tools')
.addItem('Format report', 'formatReport')
.addItem('Add GST column', 'addGstColumn')
.addSeparator()
.addSubMenu(
SpreadsheetApp.getUi().createMenu('Help')
.addItem('About', 'showAbout')
)
.addToUi();
}
शीट reload करें — Help के पास ExcelWalaa Tools नाम का नया menu दिखेगा। हर item उस function को चलाता है जिसका नाम आपने string में दिया है।
Alerts और prompts
function addGstColumn() {
const ui = SpreadsheetApp.getUi();
const response = ui.prompt('GST rate', 'Enter GST % (e.g. 3):', ui.ButtonSet.OK_CANCEL);
if (response.getSelectedButton() !== ui.Button.OK) return;
const rate = Number(response.getResponseText()) / 100;
if (isNaN(rate)) {
ui.alert('Please enter a number.');
return;
}
const confirm = ui.alert(`Add GST at ${rate * 100}%?`, ui.ButtonSet.YES_NO);
if (confirm !== ui.Button.YES) return;
// ... do the work ...
SpreadsheetApp.getActiveSpreadsheet().toast('GST column added', 'Done', 3);
}
function showAbout() {
SpreadsheetApp.getUi().alert('ExcelWalaa Tools v1.0');
}
ui.alert— message box (चाहें तो Yes/No के साथ)।ui.prompt— user से कुछ type करवाता है।toast— कोने में छोटा notification जो काम नहीं रोकता।
HTML वाला sidebar
Editor में एक HTML file जोड़ें (+ → HTML), नाम Sidebar:
<!DOCTYPE html>
<html>
<body style="font-family: Arial; padding: 12px;">
<h3>Quick entry</h3>
<input id="item" placeholder="Item name"><br><br>
<input id="amount" type="number" placeholder="Amount"><br><br>
<button onclick="save()">Add</button>
<p id="msg"></p>
<script>
function save() {
const item = document.getElementById('item').value;
const amount = Number(document.getElementById('amount').value);
google.script.run
.withSuccessHandler(() => { document.getElementById('msg').textContent = 'Saved!'; })
.addEntry(item, amount);
}
</script>
</body>
</html>
और Code.gs में:
function showSidebar() {
const html = HtmlService.createHtmlOutputFromFile('Sidebar').setTitle('Quick entry');
SpreadsheetApp.getUi().showSidebar(html);
}
function addEntry(item, amount) {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sales')
.appendRow([new Date(), item, 1, amount]);
}
google.script.run.functionName(...) से HTML page आपके server-side functions को call कर पाता है। इसे खोलने के लिए showSidebar को अपने menu में जोड़ें।
मुख्य बातें
- Ranges format करने के लिए setters chain करें; ₹ और तारीख़ों के लिए number formats लें।
onOpen+createMenuसे non-technical users को एक-click वाले buttons मिलते हैं।HtmlService+google.script.runसे Sheets के अंदर सरल forms बनते हैं।