-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.js
More file actions
61 lines (56 loc) · 1.66 KB
/
Code.js
File metadata and controls
61 lines (56 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Creates a custom menu in the Google Sheets UI.
*/
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('📊 Sparkline Helper')
.addItem('Open Sparkline Helper', 'showSparklineSidebar')
.addToUi();
}
/**
* Displays the HTML file as a Sidebar.
* This is persistent and fits perfectly into the Google Sheets workflow.
*/
function showSparklineSidebar() {
const html = HtmlService.createHtmlOutputFromFile('sidebar')
.setTitle('Sparkline Helper');
SpreadsheetApp.getUi().showSidebar(html);
}
/**
* Gets the A1 notation of the currently selected range.
*/
function getSelectedRange() {
try {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getActiveRange();
return range ? range.getA1Notation() : '';
} catch (e) {
return '';
}
}
/**
* Gets the actual values from the selection to show in the preview.
* It flattens the range and filters for numbers.
*/
function getSelectedData() {
try {
const range = SpreadsheetApp.getActiveRange();
if (!range) return [];
const values = range.getValues();
// Flatten 2D array and filter for numbers
const flat = values.reduce((acc, row) => acc.concat(row), [])
.filter(cell => typeof cell === 'number');
return flat;
} catch (e) {
return [];
}
}
/**
* Inserts the generated formula into a specific target cell.
*/
function insertFormula(formula, targetCell) {
const sheet = SpreadsheetApp.getActiveSheet();
const range = targetCell ? sheet.getRange(targetCell) : sheet.getActiveCell();
range.setFormula(formula);
SpreadsheetApp.getActive().toast(`Formula inserted at ${range.getA1Notation()}`, 'Success');
}