Created
August 26, 2022 23:21
-
-
Save esokullu/c5aa1c447d89abc7e78aefc747eb8686 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @OnlyCurrentDoc | |
*/ | |
function onOpen(e) { | |
DocumentApp.getUi().createAddonMenu() | |
.addItem('Başlat', 'showSidebar') | |
//.addItem('Türkçeleştir', 'doIt') | |
.addToUi(); | |
} | |
function onInstall(e) { | |
onOpen(e); | |
} | |
function showSidebar() { | |
const ui = HtmlService.createHtmlOutputFromFile('sidebar') | |
.setTitle('Türkçeleştir'); | |
DocumentApp.getUi().showSidebar(ui); | |
} | |
function doIt() { | |
let deasciified = getDeasciified(); | |
insertText(deasciified.translation); | |
} | |
function getSelectedText() { | |
const selection = DocumentApp.getActiveDocument().getSelection(); | |
const text = []; | |
if (selection) { | |
const elements = selection.getSelectedElements(); | |
for (let i = 0; i < elements.length; ++i) { | |
if (elements[i].isPartial()) { | |
const element = elements[i].getElement().asText(); | |
const startIndex = elements[i].getStartOffset(); | |
const endIndex = elements[i].getEndOffsetInclusive(); | |
text.push(element.getText().substring(startIndex, endIndex + 1)); | |
} else { | |
const element = elements[i].getElement(); | |
// Only translate elements that can be edited as text; skip images and | |
// other non-text elements. | |
if (element.editAsText) { | |
const elementText = element.asText().getText(); | |
// This check is necessary to exclude images, which return a blank | |
// text element. | |
if (elementText) { | |
text.push(elementText); | |
} | |
} | |
} | |
} | |
} | |
if (!text.length) throw new Error('Lütfen önce metin seçin'); | |
return text; | |
} | |
function getDeasciified() { | |
const text = getSelectedText().join('\n'); | |
return { | |
text: text, | |
translation: deasciify(text) | |
}; | |
} | |
function insertText(newText) { | |
const selection = DocumentApp.getActiveDocument().getSelection(); | |
if (selection) { | |
let replaced = false; | |
const elements = selection.getSelectedElements(); | |
if (elements.length === 1 && elements[0].getElement().getType() === | |
DocumentApp.ElementType.INLINE_IMAGE) { | |
throw new Error('Resim eklenemez.'); | |
} | |
for (let i = 0; i < elements.length; ++i) { | |
if (elements[i].isPartial()) { | |
const element = elements[i].getElement().asText(); | |
const startIndex = elements[i].getStartOffset(); | |
const endIndex = elements[i].getEndOffsetInclusive(); | |
element.deleteText(startIndex, endIndex); | |
if (!replaced) { | |
element.insertText(startIndex, newText); | |
replaced = true; | |
} else { | |
// This block handles a selection that ends with a partial element. We | |
// want to copy this partial text to the previous element so we don't | |
// have a line-break before the last partial. | |
const parent = element.getParent(); | |
const remainingText = element.getText().substring(endIndex + 1); | |
parent.getPreviousSibling().asText().appendText(remainingText); | |
// We cannot remove the last paragraph of a doc. If this is the case, | |
// just remove the text within the last paragraph instead. | |
if (parent.getNextSibling()) { | |
parent.removeFromParent(); | |
} else { | |
element.removeFromParent(); | |
} | |
} | |
} else { | |
const element = elements[i].getElement(); | |
if (!replaced && element.editAsText) { | |
// Only translate elements that can be edited as text, removing other | |
// elements. | |
element.clear(); | |
element.asText().setText(newText); | |
replaced = true; | |
} else { | |
// We cannot remove the last paragraph of a doc. If this is the case, | |
// just clear the element. | |
if (element.getNextSibling()) { | |
element.removeFromParent(); | |
} else { | |
element.clear(); | |
} | |
} | |
} | |
} | |
} else { | |
const cursor = DocumentApp.getActiveDocument().getCursor(); | |
const surroundingText = cursor.getSurroundingText().getText(); | |
const surroundingTextOffset = cursor.getSurroundingTextOffset(); | |
// If the cursor follows or preceds a non-space character, insert a space | |
// between the character and the translation. Otherwise, just insert the | |
// translation. | |
if (surroundingTextOffset > 0) { | |
if (surroundingText.charAt(surroundingTextOffset - 1) !== ' ') { | |
newText = ' ' + newText; | |
} | |
} | |
if (surroundingTextOffset < surroundingText.length) { | |
if (surroundingText.charAt(surroundingTextOffset) !== ' ') { | |
newText += ' '; | |
} | |
} | |
cursor.insertText(newText); | |
} | |
} | |
function deasciify(text) { | |
var formData = { | |
'data': text | |
}; | |
var options = { | |
'method' : 'post', | |
'payload' : formData, | |
'muteHttpExceptions': true | |
}; | |
let api = "https://{url}/deasciify"; | |
var response = UrlFetchApp.fetch(api, options); | |
var deasciified = response.getContentText(); | |
return deasciified; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment