-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
83 lines (69 loc) · 2.89 KB
/
Code.gs
File metadata and controls
83 lines (69 loc) · 2.89 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*****************************************************
* Persian Tools — شمارهگذار (Ultra Fast Final Version)
* ✅ Processes instantly (top → down)
* ✅ Doesn’t alter alignment or RTL/LTR direction
* ✅ Prefix/Suffix (default suffix ".")
* ✅ One-click apply, no waiting spinner hang
*****************************************************/
const PERSIAN_DIGITS = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'];
const PERSIAN_ALPHA = ['ا','ب','پ','ت','ث','ج','چ','ح','خ','د','ذ','ر','ز','ژ','س','ش','ص','ض','ط','ظ','ع','غ','ف','ق','ک','گ','ل','م','ن','و','ه','ی'];
const ARABIC_ALPHA = ['الف','ب','ج','د','هـ','و','ز','ح','ط','ی','ک','ل','م','ن','س','ع','ف','ص','ق','ر','ش','ت','ث','خ','ذ','ض','ظ','غ'];
// === MENU ===
function onOpen() {
DocumentApp.getUi()
.createMenu('FA_Docs')
.addItem('شمارهگذار', 'openSidebar')
.addToUi();
}
function openSidebar() {
const html = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('شمارهگذار');
DocumentApp.getUi().showSidebar(html);
}
// === HELPER: Collect all selected paragraphs (top→down) ===
function getSelectedParagraphs_() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const sel = doc.getSelection();
if (!sel) return [];
const paras = [];
const seen = new Set();
sel.getRangeElements().forEach(re => {
let el = re.getElement();
while (el && el.getType() !== DocumentApp.ElementType.PARAGRAPH &&
el.getType() !== DocumentApp.ElementType.LIST_ITEM) {
el = el.getParent();
}
if (el && !seen.has(el)) {
seen.add(el);
paras.push(el);
}
});
// Sort in document order
paras.sort((a, b) => a.getParent().getChildIndex(a) - b.getParent().getChildIndex(b));
return paras;
}
// === MAIN FUNCTION ===
function applyPersianNumbering(data) {
const paras = getSelectedParagraphs_();
if (paras.length === 0) return 'هیچ متنی انتخاب نشده است.';
const makeNum = (n) => {
if (data.style === 'persian') return String(n).replace(/\d/g, d => PERSIAN_DIGITS[d]);
if (data.style === 'faAlpha') return PERSIAN_ALPHA[(n - 1) % PERSIAN_ALPHA.length];
if (data.style === 'arAlpha') return ARABIC_ALPHA[(n - 1) % ARABIC_ALPHA.length];
return String(n);
};
const prefix = data.prefix || '';
const suffix = (data.suffix === '') ? '' : (data.suffix || '.');
const RLM = '\u200F'; // keeps Persian digits displayed RTL if needed
let count = 1;
// Directly edit text of each paragraph — fastest method
paras.forEach(p => {
let text = p.getText().trim();
if (text) {
const num = makeNum(count++);
p.setText(`${RLM}${prefix}${num}${suffix ? suffix + ' ' : ' '}${text}`);
}
});
return 'انجام شد';
}