Skip to content

Instantly share code, notes, and snippets.

@singasonggkim
Forked from hyamamoto/LENB.js
Last active August 9, 2018 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singasonggkim/b3bd58e058e6c9f45f971d006aa1d8b9 to your computer and use it in GitHub Desktop.
Save singasonggkim/b3bd58e058e6c9f45f971d006aa1d8b9 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)
* @contributor Seongji Kim (singasonggkim@gmail.com)
* @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.
if (Array.isArray(text)) {
var result = [];
for (var i = 0; i < text.length; i++) {
var temp = [];
for (var j = 0; j < text[i].length; j++) {
temp.push(_lengthOfText(text[i][j], _isByteChar));
}
result.push(temp);
}
return result;
} else {
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).
if (Array.isArray(text)) {
var result = [];
for (var i = 0; i < text.length; i++) {
var temp = [];
for (var j = 0; j < text[i].length; j++) {
temp.push(_lengthOfText(text[i][j], _isHalfChar));
}
result.push(temp);
}
return result;
} else {
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment