How to Preserve Sheet Formats and Translate? ( Google Apps Script, google sheet )

icecream sugar black

How can i Preserve Sheet Formats and Translate? I don't want to use the translation latency(1000) or translate cell by cell. it was worked well but I have a large amount of text I want to translate.

enter image description here I used the following code:

var range1 = sheet1.getRange("A:A");
text1 = range1.getValues();
translatedtext = LanguageApp.translate(text1, 'ko', 'en');

This is the starting language. from log. after getValues from sheet. If I 'setValue' this into the sheet as it is, it will be written in each cell as it is.

[안녕하세요], [사과], [], [바나나]

This is how it is translated. Line breaks are gone and replaced with commas, when pasting them into a sheet, it puts all the text in one cell.

hello, apple,, banana
Tanaike

I believe your goal is as follows.

  • You want to translate the cell values of the sheet by reducing the process cost.

In this case, how about the following modification?

Modified script 1:

In this pattern, the cell values are translated after the retrieved values were converted to the string. By this flow, LanguageApp.translate is used by one call.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1"); // Please set your sheet name.
var range = sheet.getRange("A1:A" + sheet.getLastRow())
var values = JSON.parse(LanguageApp.translate(JSON.stringify(range.getValues()), 'ko', 'en'));
range.offset(0, 1).setValues(values);
  • When this script is run, the values are retrieved from column "A", and the values are translated, and then, the translated values are put into column "B".

Modified script 2:

In this pattern, the cell values are retrieved as the CSV data, and translated it. And then, the translated values are parsed and put into the cells. By this flow, LanguageApp.translate is used by one call.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1"); // Please set your sheet name.
var url = `https://docs.google.com/spreadsheets/export?exportFormat=csv&id=${ss.getId()}&gid=${sheet.getSheetId()}`;
var res = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } });
var csv = res.getContentText();
var translated = LanguageApp.translate(csv, 'ko', 'en');
var ar = Utilities.parseCsv(translated).map(([a]) => [a]);
sheet.getRange(1, 2, ar.length).setValues(ar);
// DriveApp.getFiles(); // This comment line is used for automatically detecting the scope of Drive API.
  • When this script is run, the values are retrieved from column "A", and the values are translated, and then, the translated values are put into column "B".

  • If an error related to the scopes occurs, please include the scope of Drive API.

Reference:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to run a Google Apps Script on a hidden sheet

How to print the data of an array into a Google sheet with Google Apps Script

How to add the onOpen trigger to a Google Sheet with Google Apps Script?

Google Apps Script: Google Sheet Grouping

How to extract Google sheet formulas in sheet with a script?

Google Apps Script - How to send HTML email from Sheet?

How to extract the pubhtml ID in google sheet using Apps Script?

Google Apps Script how to export specific sheet as csv format

How to embbed a Charts.Chart to Google Sheet (Script Apps)

How to get specific sheet by id in Google Apps script?

Apps Script (Google Sheet) - How to customize the fotmat in a concatened string?

google apps script sheet Calculation control

Apps Script - Export JSON properly into Google Sheet

google apps script - batch setValues() on Sheet

get last update of google sheet in apps script

Indent pretty JSON in Google Sheet Apps Script

Running apps script in Google Sheet app

Google Apps Script print sheet with margins

Google sheet + Apps script: faster then for loop

Adjusting JS code to Apps Script in Google sheet

Google apps script - automatically filter the active sheet

Copy sheet to another spreadsheet [Google Apps Script]

Google Apps Script - setvalues not working on sheet

Max size Google sheet, with apps script

Google Apps Script Sheet extract numbers as string

Google apps script - Importing sheet values from another sheet

Script to the active Sheet on Google Sheet

How to request or get the permission of google spread sheet access in the apps script of other sheet?

Apps Script "setFormulas" in Google Sheet: how to set the correct row in reference to other sheet?