强制转换方法 asTextItem 会引发 Google 表单项错误

问题描述 投票:0回答:1

此代码是为了确保该项目可以在Google表单中以正确的类型进行操作。此代码在

TypeError: item.asTextItem is not a function
行抛出
return item.asTextItem();
。在我看来,你只能通过正确的项目类型进入该行。可能存在什么问题或解决方案?

function createFormItem_(form, itemData, config, indices, questionCount) {
  const itemHandlers = {
    'Single': createSingleChoiceItem_,
    'Multiple': createMultipleChoiceItem_,
    'ShortAnswer': createShortAnswerItem_,
    'DateOnly': createDateItem_
  };

  // Get the base type (handle Multiple variants)
  const baseType = itemData[indices.itemType].split(/\d+/)[0];
  const handler = itemHandlers[baseType] || createDefaultItem_;
  const item = handler(form, itemData, config, indices);
  
  return item;
}

function createShortAnswerItem_(form, itemData, config, indices) {
  const item = form.addTextItem();
  const validationType = itemData[indices.validationCol];
  
  const validations = {
    'Email': createEmailValidation_,
    'PhoneNumber': createPhoneValidation_
  };
  
  if (validationType in validations) {
    const validation = validations[validationType]();
    item.setValidation(validation);
  }
  
  return item;
}
function updateQuestionItem_(form, config, row, cols, existingStructureArr, questionCount, position) {
  const itemId = row[cols.id];
  const oldItemIndex = existingStructureArr.indexOf(itemId);
  
  let item;
  if (oldItemIndex >= 0) {
    const oldTitle = existingStructureArr[oldItemIndex + 1]; //String stored as ID1;Title1;ID2;Title2 => TitleIndex == IdIndex + 1
    item = GetQuestionItemInFormByTitle_(oldTitle, form);
    form.moveItem(item, position - 1);
    
    isChoiceQuestion = item.getType() == FormApp.ItemType.MULTIPLE_CHOICE || item.getType() == FormApp.ItemType.CHECKBOX;
    if(isChoiceQuestion) applyChoices_(item, row, cols, config);
  } else {
    item = createFormItem_(form, row, config, cols, questionCount)
  }

  applyCommonProperties_(item, row, cols, questionCount);
  return item;
}


function applyCommonProperties_(item, itemData, indices, questionCount) {
  const title = isNumbered ? 
    `${questionCount}. ${itemData[indices.questionCol]}` : 
    itemData[indices.questionCol];

  item = ConvertGeneric2SpecificItem_(item);
  item.setTitle(title)
      .setRequired(itemData[indices.requiredCol])
      .setHelpText(itemData[indices.itemGuide]);
}

function ConvertGeneric2SpecificItem_(item) {
  try {
    switch (item.getType()) {
      case FormApp.ItemType.MULTIPLE_CHOICE:
        return item.asMultipleChoiceItem();
      case FormApp.ItemType.CHECKBOX:
        return item.asCheckboxItem();
      case FormApp.ItemType.LIST:
        return item.asListItem();
      case FormApp.ItemType.TEXT:
        return item.asTextItem();
      case FormApp.ItemType.PARAGRAPH_TEXT:
        return item.asParagraphTextItem();
      case FormApp.ItemType.DATE:
        return item.asDateItem();
      case FormApp.ItemType.TIME:
        return item.asTimeItem();
      // Add more cases for other item types as needed
      default:
        Logger.log(`Invalid item type: ${item.getType()}`);
        return null;
    }
  } catch (error) {
    Logger.log(`Error converting item: ${error.message}`);
    return null;
  }
}
google-sheets google-apps-script google-forms
1个回答
0
投票

这是我对问题中遇到的“错误”的“答案”。该方法的目的是在代码对其执行任何操作之前确保它是正确的

ItemType
。我相信 Google 实际上没有提供将特定项目类型转换为 some 项目类型(对我来说特别是
MULTIPLE_CHOICE
TEXT
)的相同类型的方法。所以我所做的只是忽略错误并继续使用原始项目。而且效果很好。就是这个方法。

function ConvertGeneric2SpecificItem_(item)
{
  try
  {
    switch(item.getType())
    {
      case FormApp.ItemType.MULTIPLE_CHOICE:
        return item.asMultipleChoiceItem();
      case FormApp.ItemType.CHECKBOX:
        return item.asCheckboxItem();
      case FormApp.ItemType.LIST:
        return item.asListItem();
      case FormApp.ItemType.TEXT:
        return item.asTextItem();
      case FormApp.ItemType.PARAGRAPH_TEXT:
        return item.asParagraphTextItem();
      case FormApp.ItemType.DATE:
        return item.asDateItem();
      case FormApp.ItemType.TIME:
        return item.asTimeItem();
      // Add more cases for other item types as needed
      default:
        Logger.log("Invalid item type. Return original item.");
        return item;
    }
  } catch (error) {
    Logger.log(`Error converting item: ${error.message}. Return original item`);
    return item;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.