Skip to content

Instantly share code, notes, and snippets.

@hyamamoto
Last active October 13, 2021 08:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hyamamoto/e0ea24f76fa23772e651 to your computer and use it in GitHub Desktop.
Save hyamamoto/e0ea24f76fa23772e651 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.
// 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 (higon@freepress.jp)
* @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);
}
Google Doc Apps Script: LENB() / LENH() functions for Spreadsheet
Copyright (C) 2014 Hiroshi Yamamoto <higon@freepress.jp>
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.
@TyeolRik
Copy link

TyeolRik commented Jan 9, 2018

Works Well.

@singasonggkim
Copy link

singasonggkim commented Aug 9, 2018

It works, but unfortunately not in an arrayformula!
similar issue addressed here: https://stackoverflow.com/questions/48537313/custom-function-doesnt-work-with-arrayformula
so I modified the code a bit: https://gist.github.com/singasonggkim/b3bd58e058e6c9f45f971d006aa1d8b9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment