Skip to content

Instantly share code, notes, and snippets.

@silassare
Last active August 29, 2020 22:22
Show Gist options
  • Save silassare/2707e096f78da47939c2ae6f543fab99 to your computer and use it in GitHub Desktop.
Save silassare/2707e096f78da47939c2ae6f543fab99 to your computer and use it in GitHub Desktop.
This script checks for some errors in translations, and helps the translators fix that.
function onEdit (evt) {
// permet de récuperer la valeur d'une cellule
var range = evt.range;
validateRow(range.getRow(), true);
}
function translationAutoFill(csv, tab_sep="\t", line_sep="\n"){
var lines = csv.split(line_sep);
var themes_inputs = document.querySelectorAll(".input-theme-text textarea");
var sale_points_inputs = document.querySelectorAll(".input-sp-text textarea");
if (themes_inputs.length != lines.length || sale_points_inputs.length != lines.length){
throw new Error('Inputs fields and csv lines count are not equal.')
}
for(var i = 0; i < lines.length; i++){
var line_parts = lines[i].split(tab_sep);
themes_inputs[i].textContent = line_parts[0];
sale_points_inputs[i].textContent = line_parts[1];
}
}
function validateFullSheet(){
var row = 1;
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
while(String(activesheet.getRange('A' + row).getValue()).trim().length){
validateRow(row);
row++;
}
}
function validateRow (row, logEditor = false) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var activesheet = spreadsheet.getActiveSheet();
var activeUser = Session.getActiveUser();
var themeCell = activesheet.getRange('F' + row);
var sellingPointCell = activesheet.getRange('G' + row);
var errorCell = activesheet.getRange('I' + row);
var editorsCell = activesheet.getRange('K' + row);
var spaceReg = /[\s\t]+/g;
var theme = String(themeCell.getValue()).trim().replace(spaceReg, ' ');
var sellingPoint = String(sellingPointCell.getValue()).trim().replace(spaceReg, ' ');
var editors = String(editorsCell.getValue()).trim().replace(spaceReg, ' ').split('\n');
var errors = [];
var warnings = [];
if (!theme.length && !sellingPoint.length){
return;
}
// ===== THEME
if (theme.length > 75){
errors.push('- Le theme ne doit contenir plus de 75 caractères.');
}
if (theme.length < 30){
errors.push('- Le theme doit contenir minimum 30 caractères. Décrivez bien le produit.');
}
if (theme.indexOf('-') >= 0 && !/ - /.test(theme)){
errors.push('- Il semble que des espaces autour de - soient nécessaires.');
}
var _brandSpace = theme.indexOf(' - ');
if ( _brandSpace >= 0 ){
let chunk = theme.slice(_brandSpace, _brandSpace+4);
if(chunk.toUpperCase() !== chunk){
errors.push('- La lettre après la marque et - devrait être en majuscule.');
}
}
// theme first char should be uppercase
var tFistChar = theme[0];
if ( tFistChar != tFistChar.toUpperCase()){
theme[0] = tFistChar.toUpperCase();
}
// ===== SELLING POINTS
if (sellingPoint.length > 150){
errors.push('- Le selling point ne doit contenir plus de 150 caractères.');
}
if (sellingPoint.length < 30){
errors.push('- Le selling point doit contenir minimum 30 caractères. Vendez bien le produit.');
}
// selling point first char should be lowercase
var spFistChar = sellingPoint[0];
if (spFistChar != spFistChar.toLowerCase()){
sellingPoint[0] = spFistChar.toLowerCase();
}
if (/^(de|et|pour|avec) /i.test(sellingPoint)){
warnings.push("- Eviter de mettre au début du selling point: pour, de, et, avec ....");
}
// ===== ALL
// find duplicate
var maps = {};
var duplicates = [];
var wordsToIgnore = {
'de' : 1,
'pour': 1,
'avec': 1,
'et': 1,
};
var cutReg = /[ ,;.:-]/;
var words = theme.split(cutReg).concat(sellingPoint.split(cutReg));
for(var i=0; i < words.length; i++){
var w = words[i].toLowerCase();
if (w.length > 1 && !wordsToIgnore[w]){
if(maps[w]){
if(maps[w] === 1){
duplicates.push(w);
}
maps[w]++;
} else {
maps[w] = 1;
}
}
}
if (duplicates.length){
warnings.push("- Des répétitions, utilisez des synonymes, ignorez ce avertisement s'il sagit de mots de liaisons: "+ duplicates.join(', '));
}
// ===== FINISH
themeCell.setValue(theme);
sellingPointCell.setValue(sellingPoint);
if (errors.length){
errorCell.setValue(errors.concat(warnings).join('\n\n')).setBackground('#FF0000').setFontColor("#FFFFFF");
} else if (warnings.length){
errorCell.setValue(warnings.join('\n\n')).setBackground('#F48024').setFontColor("#FFFFFF");
} else {
errorCell.setValue('ACCEPTABLE!').setBackground('#188038').setFontColor('#FFFFFF');
}
if(logEditor){
var editorLogged = false;
var user = activeUser.getEmail();// + ':' + Session.getTemporaryActiveUserKey();
for(var i = 0; i< editors.length; i++){
if (editors[i] === user){
editorLogged = true;
break;
}
}
if (!editorLogged){
editors.push(user);
}
editorsCell.setValue(editors.join('\n'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment