-
Notifications
You must be signed in to change notification settings - Fork 8
/
code.gs
155 lines (146 loc) · 4.78 KB
/
code.gs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
*
* OnInstall method.
*
* Necessary for the menu items to populate the first time after the add-on is installed.
*
**/
function onInstall(e) {
onOpen(e);
}
/**
*
* Toolbar menu creation.
*
* Called on worbook opening.
*
**/
function onOpen() {
SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Start a new translation', 'showSidebar')
.addItem('About', 'showAbout')
.addToUi();
}
/*
* Example function for Google Analytics Measurement Protocol.
* @param {string} tid Tracking ID / Web Property ID
* @param {string} url Document location URL
*/
function sendGAMP(tid, url) {
var data = {
'v': '1',
'tid': tid,
'cid': Utilities.getUuid(),
'z': Math.floor(Math.random() * 10E7),
't': 'pageview',
'dl': url
};
var payload = Object.keys(data).map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
}).join('&');
var options = {
'method': 'POST',
'payload': payload
};
UrlFetchApp.fetch('http://www.google-analytics.com/collect', options);
}
/**
*
* Sidebar title, content & size.
*
**/
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Translate My Sheet')
.setWidth(300);
// Open sidebar
SpreadsheetApp.getUi().showSidebar(html);
}
function showAbout() {
var html = HtmlService.createHtmlOutputFromFile('about')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('About')
.setWidth(250)
.setHeight(450);
SpreadsheetApp.getActive().show(html);
}
/**
*
* Translate function.
*
**/
function translate(radioFull, radioSelected, radioOgSheet, radioNewSheet, sourceLangage, targetLangage) {
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = activeSpreadsheet.getActiveSheet();
var activeRange = activeSheet.getActiveRange().getA1Notation();
activeSpreadsheet.toast("Translation in progress...", "", -1);
try {
if (radioOgSheet) {
var targetSheet = activeSheet
} else if (radioNewSheet) {
var newName = activeSheet.getName() + " - " + targetLangage;
if (activeSpreadsheet.getSheetByName(newName)) {
var sheets = activeSpreadsheet.getSheets();
var counter = 1;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].getName().indexOf(newName) != -1) {
counter++;
}
}
newName += counter;
}
var targetSheet = activeSpreadsheet.duplicateActiveSheet().setName(newName);
targetSheet.setTabColor("1E824C");
}
var activeCell = activeSheet.getActiveCell();
if (radioFull) {
translateFullPage(targetSheet, sourceLangage, targetLangage);
} else if (radioSelected) {
translateSelectedCells(targetSheet, activeRange, sourceLangage, targetLangage);
}
activeSpreadsheet.toast("Done.", "", 3);
} catch (err) {
activeSpreadsheet.toast("An error occured:" + err);
}
}
/**
*
* Code for translate full page content from a source to a target langage.
*
**/
function translateFullPage(targetSheet, sourceLangage, targetLangage) {
var lrow = targetSheet.getLastRow();
var lcol = targetSheet.getLastColumn();
for (var i = 1; i <= lrow; i++) {
for (var j = 1; j <= lcol; j++) {
if (targetSheet.getRange(i, j).getValue() != "") {
var activeCellText = targetSheet.getRange(i, j).getValue();
// TODO: here, wrap the call in a try catch
var activeCellTranslation = LanguageApp.translate(activeCellText, sourceLangage, targetLangage);
targetSheet.getRange(i, j).setValue(activeCellTranslation);
}
}
}
}
/**
*
* Code for translate only selected range content in a sheet from a source to a target langage.
*
**/
function translateSelectedCells(targetSheet, activeRange, sourceLangage, targetLangage) {
var range = targetSheet.getRange(activeRange);
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
var activeCellText = range.getCell(i, j).getValue();
// TODO: here, wrap the call in a try catch
var activeCellTranslation = LanguageApp.translate(activeCellText, sourceLangage, targetLangage);
range.getCell(i, j).setValue(activeCellTranslation);
range.getCell(i, j).setBackground("#1E824C");
range.getCell(i, j).setFontColor("#FFFFFF");
}
}
}