GASで「TypeError: ○○ is not a function」と出るときの直し方(getMonth・split・getValue)

記事
IT・テクノロジー
GAS を動かしたら「TypeError: sheet.getRange(...).getValue(...).getMonth is not a function」のような赤い文字が出て止まった。関数の名前は合っているのに「関数ではない」と言われるので、どこが悪いのか分かりにくいエラーです。

このエラーは、ほとんどの場合「その変数の中身が、思っていた種類と違う」ことが原因です。日付のつもりが文字だった、文字のつもりが数値だった、1 つの値のつもりが表(配列)だった、のどれかです。

■ エラーの文の「○○」で原因を見分ける
・getMonth / getFullYear / getDate is not a function:日付のつもりのセルが「文字」です。シートでは日付に見えても、手入力やコピーで文字のまま入っていることがあります
・split / trim / replace / indexOf is not a function:文字のつもりのセルが「数値」か「日付」です。電話番号・郵便番号・社員番号がよくこうなります
・getValue / getValues is not a function:getValues() で取り出したあとの「表(配列)」に、もう一度 getValue を付けています

■ 直す前(3 つとも止まる書き方)
function oldMonthOfRow(row) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('受付');
  return sheet.getRange(row, 1).getValue().getMonth() + 1;
}
function oldFirstName(row) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('受付');
  return sheet.getRange(row, 2).getValue().split(' ')[0];
}
function oldTotal() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('受付');
  const values = sheet.getRange(2, 3, sheet.getLastRow() - 1, 1).getValues();
  return values.getValue();
}

■ 直したあと(中身の種類をそろえてから使う)
日付は toDate、文字は toText を通してから使います。どちらも、セルが日付・文字・数値・空のどれでも止まりません。

function isDate(v) { return Object.prototype.toString.call(v) === '[object Date]' && !isNaN(v.getTime()); }

function toDate(v) {
  if (isDate(v)) return v;
  if (typeof v === 'number') return new Date(Math.round((v - 25569) * 86400000)); // シリアル値
  const m = String(v == null ? '' : v).trim().match(/^(\d{4})[\/\-年.](\d{1,2})[\/\-月.](\d{1,2})/);
  return m ? new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3])) : null;
}

function toText(v) {
  if (v == null) return '';
  if (isDate(v)) return v.getFullYear() + '/' + (v.getMonth() + 1) + '/' + v.getDate();
  return String(v);
}

function monthOfRow(row) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('受付');
  const d = toDate(sheet.getRange(row, 1).getValue());
  return d ? d.getMonth() + 1 : null; // 日付として読めない行は null
}

function firstName(row) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('受付');
  return toText(sheet.getRange(row, 2).getValue()).trim().split(/[  ]+/)[0];
}

function total() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('受付');
  const last = sheet.getLastRow();
  if (last < 2) return 0;
  return sheet.getRange(2, 3, last - 1, 1).getValues()
    .reduce(function (sum, r) { const n = Number(r[0]); return sum + (isNaN(n) ? 0 : n); }, 0);
}

■ 使い方のこつ
・toDate は「2026/12/04」「2026-12-4」「2026年12月4日」の文字と、シリアル値(46360 のような数)を日付に直します。読めないときは null を返すので、if (!d) で飛ばせます
・firstName は半角・全角どちらの空白でも区切ります。名前が数値でも「12345」の文字として扱います
・total は「2,000円」のように数値でないセルを飛ばします。足したいときは、先に replace(/[,円]/g, '') で数字だけにしてください
・getValues() の結果は「行の配列の配列」です。1 つ目の値は values[0][0] のように取り出します

■ 自分で直すのが大変なときは
うまく動かないときは、エラー1件3,000円で直します(ご購入前の相談は無料)。エラーの文を送っていただければ、購入の前に直せそうかをお答えします。
・動かない GAS・VBA のエラーを直します:https://coconala.com/services/4455210

あわせて読みたい:
・GASで「Cannot read properties of null」と出るときの直し方 https://coconala.com/blogs/6357864/848320
・GASで日付が「Wed Nov 18 2026…」や前の日になるときの直し方 https://coconala.com/blogs/6357864/849064

この記事は、AI(Claude)の助けを借りて書いています。コードは、テスト用のデータで動きを確かめてから載せています。

■ 原因のセルを探す:どのセルが原因かを 1 回で見つける点検関数
上の 3 つに当てはまらないときや、何百行もあってどの行で止まっているか分からないときに使う関数です。貼って checkCells を実行すると、シートの全セルについて「日付」「文字」「数値」「空の文字」「null」などの正体をログに 1 行ずつ出します。エラーの直前の変数を whatIs(変数) で調べることもできます。

function whatIs(v) {
  if (v === null) return 'null(何も入っていない。シート名や範囲の指定を確認)';
  if (v === undefined) return 'undefined(変数に値が入っていない。配列の番号や関数の戻り値を確認)';
  if (isDate(v)) return '日付(Date)';
  if (Array.isArray(v)) {
    if (v.length && Array.isArray(v[0])) return '2次元の配列(' + v.length + '行×' + v[0].length + '列。getValues の結果。値は v[行][列] で取り出す)';
    return '配列(' + v.length + '個)';
  }
  if (typeof v === 'object' && typeof v.getValues === 'function') return 'セル範囲(Range。値は .getValue() / .getValues() で取り出す)';
  if (typeof v === 'object' && typeof v.getRange === 'function') return 'シート(Sheet)';
  if (typeof v === 'string') return v === '' ? '空の文字' : '文字(' + JSON.stringify(v.slice(0, 20)) + ')';
  if (typeof v === 'number') return '数値(' + v + ')';
  if (typeof v === 'boolean') return 'チェックボックスの値(' + v + ')';
  return typeof v;
}

function checkCells() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('受付');
  const values = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
  const out = [];
  values.forEach(function (r, i) {
    r.forEach(function (v, j) { out.push((i + 1) + '行' + (j + 1) + '列:' + whatIs(v)); });
  });
  Logger.log(out.join('\n'));
  return out;
}

■ ログの読み方
・日付の列に「文字("2026/12/04")」が混ざっていたら、その行が getMonth で止まる原因です。toDate を通すか、シートの表示形式を日付に直します
・名前や番号の列に「数値(12345)」があれば、split で止まる原因です。toText を通します
・Logger.log(whatIs(values)) で「2次元の配列(3行×2列)」と出たら、getValue ではなく values[行][列] で取り出します
・「セル範囲(Range)」と出たら、まだ値を取り出していません。.getValue() を付けます
サービス数100万件のスキルマーケット、あなたにぴったりのサービスを探す