समझिए
1. Variables
const gstRate = 0.03; // const: value won't change
let total = 0; // let: value can change
total = total + 500;
आमतौर पर const लें; जब value बदलनी हो तब let। पुराना var इस्तेमाल न करें।
2. Data types
| Type | उदाहरण | शीट में बराबर |
|---|---|---|
| Number | 1250.5 |
numeric cell |
| String | 'Ring' |
text cell |
| Boolean | true / false |
checkbox |
| Date | new Date() |
date cell |
| खाली | '' |
blank cell (Sheets null नहीं, '' देता है) |
Template literals से text बनाना आसान होता है:
const name = 'Rahul';
const amount = 4500;
const msg = `Dear ${name}, your bill is ₹${amount}.`;
3. Functions
Function steps का एक block है जिसे बार-बार इस्तेमाल कर सकते हैं — जैसे आपका अपना बनाया formula।
function addGst(amount, rate) {
return amount + amount * rate;
}
const finalAmount = addGst(10000, 0.03); // 10300
Arrow function का छोटा रूप (examples में दिखेगा):
const addGstShort = (amount, rate) => amount * (1 + rate);
4. Arrays — और code में शीट कैसी दिखती है
Array एक क्रमवार list है:
const items = ['Ring', 'Chain', 'Bangle'];
console.log(items[0]); // 'Ring' (counting starts at 0!)
console.log(items.length); // 3
शीट से कोई range पढ़ने पर आपको rows का array मिलता है, और हर row cells का array होती है:
// Range A1:C3 comes back as:
const data = [
['Item', 'Qty', 'Price'], // row 1 → data[0]
['Ring', 2, 15000], // row 2 → data[1]
['Chain', 1, 42000] // row 3 → data[2]
];
console.log(data[1][0]); // 'Ring' → row 2, column A
console.log(data[2][2]); // 42000 → row 3, column C
ध्यान दें: शीट की rows और columns 1 से शुरू होती हैं, arrays 0 से। data[1][0] का मतलब cell A2 है।
5. Objects
Object नाम वाली values को एक साथ रखता है — जैसे headings वाली एक row:
const order = { item: 'Ring', qty: 2, price: 15000 };
console.log(order.item); // 'Ring'
console.log(order.qty * order.price); // 30000
6. Conditions
if (qty <= reorderLevel) {
console.log('Reorder needed');
} else if (qty <= reorderLevel * 2) {
console.log('Running low');
} else {
console.log('Stock OK');
}
तुलना: === बराबर, !== बराबर नहीं, >, <, >=, <=। जोड़ने के लिए && (और), || (या)। हमेशा === इस्तेमाल करें, == नहीं।
7. Loops
for (let i = 1; i < data.length; i++) { // start at 1 to skip the header row
const row = data[i];
console.log(row[0] + ' → ' + row[1] * row[2]);
}
// Shorter, when you don't need the index:
for (const row of data.slice(1)) {
console.log(row[0]);
}
8. रोज़ काम आने वाले array helpers
const rows = data.slice(1); // drop header
const expensive = rows.filter(r => r[2] > 20000); // like FILTER()
const names = rows.map(r => r[0]); // pick one column
const totalValue = rows.reduce((sum, r) => sum + r[1] * r[2], 0); // like SUMPRODUCT()
मुख्य बातें
- शीट data = rows का array;
data[row][col], दोनों 0 से शुरू। - आमतौर पर
const, बदलना हो तोlet, तुलना के लिए===। filter,map,reduceज़्यादातर loops की जगह ले लेते हैं।