URL Encode with Shift-JIS using Google Apps Script


This is a sample script for achieving the URL encode with Shift-JIS using Google Apps Script.

Unfortunately, there are no methods for directly achieving above in the methods of Google Apps Script. So it is required to prepare it as the script. In order to use Shift-JIS of the character set at Google Apps Script, it is required to use it as the binary data. Because, when the value of Shift-JIS is retrieved as the string by Google Apps Script, the character set is automatically changed to UTF-8. Please be careful this.

Sample script

function urlEncodeBySJIS(str) {
  var forAsciiConvert =
  var conv = Utilities.newBlob(forAsciiConvert)
    .map(function(e) {
      return ("0" + (e & 0xff).toString(16)).slice(-2);
  var bytes = Utilities.newBlob("")
    .setDataFromString(str, "Shift_JIS")
  return (res = bytes
    .map(function(byte) {
      var n = ("0" + (byte & 0xff).toString(16)).slice(-2);
      return conv.indexOf(n) != -1
        ? String.fromCharCode(
            parseInt(n[0], 16).toString(2).length == 4
              ? parseInt(n, 16) - 256
              : parseInt(n, 16)
        : ("%" + n).toUpperCase();

// Please run this function.
function main() {
  var str = "本日は晴天なり";
  var res = urlEncodeBySJIS(str);
  Logger.log(res); // %96%7B%93%FA%82%CD%90%B0%93V%82%C8%82%E8


You can see the following result at the log.



The flow of this script is as follows.

  1. Create new blob as the empty data.
  2. Put the text value of 本日は晴天なり to the blob. At that time, the text value is put as Shift-JIS of the the character set.
    • In this case, even when blob.getDataAsString("Shift_JIS") is used, the result becomes UTF-8. So the blob is required to be used as the binary data without converting to the string data. This is the important point in this answer.
  3. Convert the blob to the byte array.
  4. Convert the bytes array of the signed hexadecimal to the unsigned hexadecimal.
    • At Google Apps Script, the byte array is uses as he signed hexadecimal. So it is required to convert to the unsigned hexadecimal.
    • When the value is the KANJI character, when the characters of 2 bytes can be converted to the string value as the ascii code, the string value is required to be used. The script can be used for this situation.
    • In the case of 本日は晴天なり, becomes %93V.
  5. Add % to the top character of each byte.