- You want to translate Japanese to English for all cells of all sheets in a Spreadsheet.
- You want to copy the sheet with the translated values to the same Spreadsheet.
- You want to achieve this using Google Apps Script.
I could understand like above. If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
In this answer, Class LanguageApp is used.
Pattern 1:
In this pattern, each cell is translated with LanguageApp.translate().
Sample script:
Please copy and paste the following script to the container-bound script of the Spreadsheet. And run myFunction at the script editor. By this, all cells of all sheets in the Spreadsheet are translated from Japanese to English. And the translated values are put to the inserted sheet in the same Spreadsheet.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
sheets.forEach(sheet => {
const s = sheet.copyTo(ss).setName(`translated_${sheet.getSheetName()}`);
const range = s.getDataRange();
const translatedValues = range.getDisplayValues().map(r => r.map(c => {
Utilities.sleep(1000);
return LanguageApp.translate(c, "ja", "en");
}));
range.setValues(translatedValues);
});
}
- In this case, each cell is translated. So from the specification,
Utilities.sleep(1000) (waiting 1 second) is required to be used. By this, when there are a lot of cells, the process time might be long.
Pattern 2:
In this pattern, all cells from a sheet are retrieved and converted to a string value, then the string value is translated. And the translated values are put to the sheet.
Sample script:
Please copy and paste the following script to the container-bound script of the Spreadsheet. And run myFunction at the script editor. By this, all cells of all sheets in the Spreadsheet are translated from Japanese to English. And the translated values are put to the inserted sheet in the same Spreadsheet.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
sheets.forEach(sheet => {
const s = sheet.copyTo(ss).setName(`translated_${sheet.getSheetName()}`);
const range = s.getDataRange();
const delimiter = "#";
const sourceValues = range.getDisplayValues().map(r => r.join(delimiter)).join("\n");
const translatedValues = Utilities.parseCsv(LanguageApp.translate(sourceValues, "ja", "en"), delimiter.charCodeAt(0));
range.setValues(translatedValues);
Utilities.sleep(1000);
});
}
- In this case, the values of all cells are translated by one request of
LanguageApp.translate(). By this, the process time will be shorter than the pattern 1. But if the values are large, an error might occur. About this, I'm not sure. I apologize for this.
- In above script,
# is used as the delimiter for converting to the string value. If # is used in the cells, please change the character.
Note:
- This modified script is run with enabling V8.
Reference:
I have to do it for each and every row, in this case, all cells have to translate? 2. I cannot understand aboutIt would be better if its converted into a separate sheet. - Tanaikea separate sheet, I couldn't understand whether OP wants to copy the source Spreadsheet to other Spreadsheet or wants to copy one of sheets to the same Spreadsheet. Unfortunately, I cannot still understand about this. I have to apologize for this. - Tanaike