Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add process to determine byte length of text (#36) #37

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions src/sheetsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const UP_KEY_TARGET_LOCALE = 'targetLocale'; // User property key for saving the
const DEEPL_API_VERSION = 'v2'; // DeepL API version
const DEEPL_API_BASE_URL_FREE = `https://api-free.deepl.com/${DEEPL_API_VERSION}/`;
const DEEPL_API_BASE_URL_PRO = `https://api.deepl.com/${DEEPL_API_VERSION}/`;
const ROW_SEPARATOR = ',,,';

// Threshold value of the length of the text to translate, in bytes. See https://developers.google.com/apps-script/guides/services/quotas#current_limitations
const THRESHOLD_BYTES = 1900;

/**
* The JavaScript object of a DeepL-supported language.
Expand Down Expand Up @@ -219,6 +223,10 @@ function setLanguage(): void {
}
}

/**
* Translate the selected cell range using DeepL API
* and paste the result in the adjacent range.
*/
function translateRange(): void {
const ui = SpreadsheetApp.getUi();
const activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
Expand Down Expand Up @@ -250,16 +258,38 @@ function translateRange(): void {
throw new Error(`[${ADDON_NAME}] Translation canceled.`);
}
}

// Get the source text
const sourceText = selectedRange
.getValues()
.map((row) => row.join(',,,,,,,,,,'));
// Translate
const translatedText = deepLTranslate(
sourceText,
userProperties[UP_KEY_SOURCE_LOCALE],
userProperties[UP_KEY_TARGET_LOCALE]
).map((row) => row.split(',,,,,,,,,,'));
const sourceTextArr = selectedRange.getValues();
const translatedText = sourceTextArr.map((row) => {
const joinedRowText = row.join(ROW_SEPARATOR);
if (getBytes(encodeURIComponent(joinedRowText)) > THRESHOLD_BYTES) {
return row.map((cellValue: string | number | boolean) => {
if (getBytes(encodeURIComponent(cellValue)) > THRESHOLD_BYTES) {
throw new Error(
`[${ADDON_NAME}] Cell content is too long. Please consider splitting the content into multiple cells:\n${cellValue}`
);
} else {
Utilities.sleep(1000); // Interval to avoid concentrated access to API
// Cell-based translation
return deepLTranslate(
cellValue.toString(),
userProperties[UP_KEY_SOURCE_LOCALE],
userProperties[UP_KEY_TARGET_LOCALE]
);
}
});
} else {
Utilities.sleep(1000); // Interval to avoid concentrated access to API
// Row-based translation
return deepLTranslate(
joinedRowText,
userProperties[UP_KEY_SOURCE_LOCALE],
userProperties[UP_KEY_TARGET_LOCALE]
)[0].split(ROW_SEPARATOR);
}
});

// Set translated text in target range
targetRange.setValues(translatedText);
// Complete
Expand Down Expand Up @@ -379,6 +409,15 @@ function getDeepLApiBaseUrl(apiKey: string): string {
: DEEPL_API_BASE_URL_PRO;
}

/**
* Get the length of a given string in bytes.
* @param text The string of which to get the bytes.
* @returns The length of the given text in bytes.
*/
function getBytes(text: string): number {
return Utilities.newBlob(text).getBytes().length;
}

if (typeof module === 'object') {
module.exports = {
onInstall,
Expand Down