This program is a script that translates all the comments in the active sheet of a Google Spreadsheet from various languages to English.
Used Technologies
- Google Apps Script
- Google Spreadsheet API
- LanguageApp for Google Apps Script
Features
- Scan comments attached to all cells in the spreadsheet.
- Translate comments from various languages into English.
Sauce Code
JavaScript
function translateComments() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getDataRange();
var comments = range.getNotes();
var sourceLanguage = Browser.inputBox('Enter the source language code (e.g., "ja" for Japanese, "fr" for French)');
for (var i = 0; i < comments.length; i++) {
for (var j = 0; j < comments[i].length; j++) {
if (comments[i][j]) {
var translation = translateToEnglish(comments[i][j], sourceLanguage);
sheet.getRange(i + 1, j + 1).setNote(translation);
}
}
}
}
function translateToEnglish(text, sourceLanguage) {
try {
var translation = LanguageApp.translate(text, sourceLanguage, 'en');
return translation;
} catch (error) {
Logger.log("Translation error: " + error);
return text; // If translation fails, return the original text
}
}
for (let i = 0; i < 10; i++) {
console.log(i);
}
// MIT License
// Copyright (c) 2023 [ANJI]
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
How to Use
- Open Google Spreadsheet: Start by opening the Google Spreadsheet that contains the comments you want to translate.
- Open Google Apps Script: Navigate to Extensions > Apps Script to open a new Apps Script editor.
- Copy the Code: Copy the prepared script and paste it into the Apps Script editor.
- Save the Script: Go to File > Save to save your script.
- Run the Script: Click the run button on the Apps Script editor, or go to Run > Run function.
- Language Selection: Note that after running the script, you must return to the Google Spreadsheet window from the Apps Script interface for the language selection text box to appear.
- Translation Complete: Once you’ve selected and confirmed the language, your comments will be translated from the specified language into English.
コメント