diff --git a/src/sheetsl.ts b/src/sheetsl.ts index a65e9be..181f919 100644 --- a/src/sheetsl.ts +++ b/src/sheetsl.ts @@ -20,7 +20,6 @@ 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; @@ -270,37 +269,23 @@ export function translateRange(): void { const sourceTextArr = selectedRange.getValues(); // console.log(`sourceTextArr: ${JSON.stringify(sourceTextArr)}`); - const translatedText = sourceTextArr.map((row) => { - const joinedRowText = row.join(ROW_SEPARATOR); - if (getBlobBytes(encodeURIComponent(joinedRowText)) > THRESHOLD_BYTES) { - console.info( - `[${ADDON_NAME}] ${joinedRowText} is longer than ${THRESHOLD_BYTES} bytes. Switching to per cell translation.` - ); - return row.map((cellValue: string | number | boolean) => { - if (getBlobBytes(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] - )[0]; - } - }); - } 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); - } - }); + const translatedText = sourceTextArr.map((row) => + row.map((cellValue: string | number | boolean) => { + if (getBlobBytes(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] + )[0]; + } + }) + ); // console.log(`translatedText: ${JSON.stringify(translatedText)}`); // Set translated text in target range