Skip to content

Instantly share code, notes, and snippets.

@SC-One
Created July 3, 2026 09:11
Show Gist options
  • Select an option

  • Save SC-One/1575fb4e31d4a9ddf2d8a29b3aaf70d7 to your computer and use it in GitHub Desktop.

Select an option

Save SC-One/1575fb4e31d4a9ddf2d8a29b3aaf70d7 to your computer and use it in GitHub Desktop.
FarsiCorrection
function onOpen() {
DocumentApp.getUi()
.createMenu("Persian Keyboard")
.addItem("Convert Selection", "convertSelection")
.addToUi();
}
function convertSelection() {
const map = {
'q':'ض','w':'ص','e':'ث','r':'ق','t':'ف','y':'غ','u':'ع','i':'ه','o':'خ','p':'ح',
'[':'ج',']':'چ',
'a':'ش','s':'س','d':'ی','f':'ب','g':'ل','h':'ا','j':'ت','k':'ن','l':'م',
';':'ک',"'":'گ',
'z':'ظ','x':'ط','c':'ز','v':'ر','b':'ذ','n':'د','m':'پ',
',':'و','.':'.','/':'/',
'Q':'َ','W':'ً','E':'ُ','R':'ٌ','T':'ُ','Y':'ِ','U':'َ','I':'ّ','O':']','P':'[',
'{':'}','}':'{',
'A':'ؤ','S':'ئ','D':'ي','F':'إ','G':'أ','H':'آ','J':'ة','K':'»','L':'«',
':':':','"':'"',
'Z':'ك','X':'ٓ','C':'ژ','V':'ٰ','B':'‌','N':'ٔ','M':'ء',
'<':'>','>':'<','?':'؟'
};
const selection = DocumentApp.getActiveDocument().getSelection();
if (!selection) {
DocumentApp.getUi().alert("Select some text first.");
return;
}
const elements = selection.getRangeElements();
elements.forEach(el => {
const element = el.getElement();
if (element.editAsText === undefined) return;
const text = element.asText();
const start = el.isPartial() ? el.getStartOffset() : 0;
const end = el.isPartial() ? el.getEndOffsetInclusive() : text.getText().length - 1;
if (start < 0 || end < 0) return;
const selected = text.getText().substring(start, end + 1);
const converted = [...selected]
.map(ch => map[ch] ?? ch)
.join("");
text.deleteText(start, end);
text.insertText(start, converted);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment