-
-
Save ymkjp/f1b566bb800d1e0f5b92 to your computer and use it in GitHub Desktop.
Google Doc & Spreadsheets's LENB() function which is present in Microsoft Excel. LENB() returns a number of bytes, while LENH() returns a width of the given text. These functions are useful when you check if your translation resources can be fit inside original screens.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To add these functions to your Google Doc documents, | |
// see https://developers.google.com/apps-script/guides/sheets/functions | |
/** | |
* @fileOverview Google Doc Apps Script: LENB() / LENH() functions for Spreadsheet. | |
* @author Hiroshi Yamamoto ([email protected]) | |
* @license <a href="http://www.wtfpl.net/">WTFPL version 2.0</a> | |
* @version 0.2.0 | |
*/ | |
/** | |
* Is the given character a byte length? | |
* @param {string} {ch} a character to be examined. | |
* @return {boolean} True if the given letter is a byte width. | |
* @private | |
*/ | |
function _isByteChar(ch) { | |
return ch < 127; | |
} | |
/** | |
* Is the given character a half-width one? | |
* @param {string} {ch} a character to be examined. | |
* @return {boolean} True if the given letter is a half-width. | |
* @private | |
*/ | |
function _isHalfChar(ch) { | |
// NOTE: Japanese half-width characters are traditionally treated as 1-byte character. | |
return _isByteChar(ch) || | |
// Half-width characters (sjis) | |
(ch == 0xf8f0) || (ch >= 0xff61 && ch < 0xffa0) || | |
// Half-width characters (unicode) | |
(ch >= 0xf8f1 && ch < 0xf8f4); | |
} | |
/** | |
* Counts the given text. | |
* @return {number} an integer value. | |
* @private | |
*/ | |
function _lengthOfText(text, predicate) { | |
var lenb = text.length; | |
for (var i = 0; i < text.length; i++) | |
if (!predicate(text.charCodeAt(i))) lenb++; | |
return lenb; | |
} | |
/** | |
* Returns the number of bytes used to represent the characters in a text string. | |
* | |
* @param {string} text The text which length you want to find. Spaces count as characters. | |
* @return {string} the number of bytes in a text. | |
* @customfunction | |
*/ | |
function LENB(text) { | |
// @summary Returns the number of bytes in a text. | |
return _lengthOfText(text, _isByteChar); | |
} | |
/** | |
* Returns the width of a given text string. Each half-width character counts as 1, | |
* full-width character counts as 2. | |
* | |
* @param {string} text The text which length you want to find. Spaces count as characters. | |
* @return {string} The text which length you want to find. Spaces count as characters. | |
* @customfunction | |
*/ | |
function LENH(text) { | |
// @summary Returns the width of a text (Counts as half-width letters). | |
return _lengthOfText(text, _isHalfChar); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Google Doc Apps Script: LENB() / LENH() functions for Spreadsheet | |
Copyright (C) 2014 Hiroshi Yamamoto <[email protected]> | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment