【GAS】GoogleフォームのgetTypeでタイプ/型が取得できないときの対処法|Logging output too large. Truncating output. { toString: [Function: toString]

Google form フォーム GAS-prograshi(プロぐらし)-kv AppsScript
記事内に広告が含まれていることがあります。

Googleフォームで取得したアイテムに対してgetTypeメソッドを使うと上手く型が取得できないことがあります。

ここではその対処法について解説しています。


getTypeで型が取得できない事例

フォームから取得した個々のアイテムに対してgetTypeメソッドを使うと、出力結果が以下のようになることがあります。

Logging output too large. Truncating output. { toString: [Function: toString],
  name: [Function: toString],
  toJSON: [Function: toString],
  ordinal: [Function: ordinal],
  compareTo: [Function: compareTo],
  CHECKBOX: [Circular],
  CHECKBOX_GRID: 
   { toString: [Function: toString],
     name: [Function: toString],
     toJSON: [Function: toString],
     ordinal: [Function: ordinal],
     compareTo: [Function: compareTo],
     CHECKBOX: [Circular],
     CHECKBOX_GRID: [Circular],
     DATE: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Circular],
        DATETIME: [Object],
        DURATION: [Object],
        GRID: [Object],
        IMAGE: [Object],
        LIST: [Object],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Object],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     DATETIME: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Circular],
        DURATION: [Object],
        GRID: [Object],
        IMAGE: [Object],
        LIST: [Object],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Object],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     DURATION: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Object],
        DURATION: [Circular],
        GRID: [Object],
        IMAGE: [Object],
        LIST: [Object],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Object],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     GRID: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Object],
        DURATION: [Object],
        GRID: [Circular],
        IMAGE: [Object],
        LIST: [Object],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Object],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     IMAGE: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Object],
        DURATION: [Object],
        GRID: [Object],
        IMAGE: [Circular],
        LIST: [Object],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Object],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     LIST: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Object],
        DURATION: [Object],
        GRID: [Object],
        IMAGE: [Object],
        LIST: [Circular],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Object],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     MULTIPLE_CHOICE: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Object],
        DURATION: [Object],
        GRID: [Object],
        IMAGE: [Object],
        LIST: [Object],
        MULTIPLE_CHOICE: [Circular],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Object],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     PAGE_BREAK: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Object],
        DURATION: [Object],
        GRID: [Object],
        IMAGE: [Object],
        LIST: [Object],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Circular],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Object],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     PARAGRAPH_TEXT: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Object],
        DURATION: [Object],
        GRID: [Object],
        IMAGE: [Object],
        LIST: [Object],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Circular],
        SCALE: [Object],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     SCALE: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Object],
        DURATION: [Object],
        GRID: [Object],
        IMAGE: [Object],
        LIST: [Object],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Circular],
        SECTION_HEADER: [Object],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     SECTION_HEADER: 
      { toString: [Function: toString],
        name: [Function: toString],
        toJSON: [Function: toString],
        ordinal: [Function: ordinal],
        compareTo: [Function: compareTo],
        CHECKBOX: [Circular],
        CHECKBOX_GRID: [Circular],
        DATE: [Object],
        DATETIME: [Object],
        DURATION: [Object],
        GRID: [Object],
        IMAGE: [Object],
        LIST: [Object],
        MULTIPLE_CHOICE: [Object],
        PAGE_BREAK: [Object],
        PARAGRAPH_TEXT: [Object],
        SCALE: [Object],
        SECTION_HEADER: [Circular],
        TEXT: [Object],
        TIME: [Object],
        VIDEO: [Object],
        FILE_UPLOAD: [Object] },
     TEXT: 
      { toString: [Function: toString],
        name: 


「CHECKBOX」や「GRID」、「TEXT」などの型が返ることを想定しているのにおかしいな、、、となることがあります。


なお、Logging output too large. Truncating output.は「ホントはもっと長いんだけど、仕様で表示しきれないから後ろの方は省略するよ」という意味です。


上記のようになるコードの例

function editForm() {
  const formId = "フォームのID";
  const form = FormApp.openById( formId );

  const items = form.getItems();

  items.forEach(item => {
    const itemType = item.getType();
    console.log(itemType);
  })
}



対処法

対処法はとっても簡単です。getTypeの後に、nameメソッドやtoStringメソッドをチェーンします。

item.getType().name()
item.getType().toString()
item.getType().toJSON()


どのメソッドを使っても、「CHECKBOX」や「GRID」、「TEXT」などの型が返ります。


実例

実例として正しく動くコードを記しておきます。

function editForm() {
  const formId = "フォームのID";
  const form = FormApp.openById( formId );

  const items = form.getItems();

  items.forEach(item => {
    const itemType = item.getType().name();
    console.log(itemType);
  })
}


タイトルとURLをコピーしました