You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Google App Script调整Google Form中项目的顺序

Reordering Google Form Items After Duplication

Absolutely! You can easily reorder items in your Google Form programmatically using Google Apps Script's Form Service—here's how to do it, tailored to your use case of reordering duplicated items:

Key Methods to Adjust Item Order

Google Apps Script provides two straightforward ways to rearrange form items:

1. Set a Specific Position with setIndex()

Every form item has a setIndex() method that lets you define its exact position (using a 0-based index). This is great if you know exactly where you want the duplicated item to go.

Example Code:

function duplicateAndSetPosition() {
  // Replace with your Form ID
  const form = FormApp.openById('YOUR_FORM_ID');
  
  // Get the item you want to duplicate (e.g., the first item in the form)
  const sourceItem = form.getItems()[0];
  
  // Duplicate the item—this returns the new duplicated item object
  const duplicatedItem = sourceItem.duplicate();
  
  // Move the duplicated item to the 3rd position (index = 2, since we start counting at 0)
  duplicatedItem.setIndex(2);
}

2. Move Relative to Another Item with moveTo()

If you prefer to position the duplicated item relative to an existing item (e.g., "before the second question" or "after the dropdown"), use the moveTo() method chain. This avoids having to track exact indices.

Example Code:

function duplicateAndMoveRelative() {
  const form = FormApp.openById('YOUR_FORM_ID');
  const sourceItem = form.getItems()[0];
  const duplicatedItem = sourceItem.duplicate();
  
  // Get the target item you want to position relative to
  const targetItem = form.getItems()[1]; // Second item in the form
  
  // Move the duplicated item BEFORE the target item
  duplicatedItem.moveTo().before(targetItem);
  
  // OR move it AFTER the target item:
  // duplicatedItem.moveTo().after(targetItem);
}

Important Notes

  • Index Boundaries: When using setIndex(), make sure the index is between 0 and form.getItems().length - 1. If you use an index larger than the total number of items, the item will default to the end.
  • Automatic Index Adjustments: When you move or reindex an item, Google Forms automatically adjusts the indices of all other items—you don’t need to update them manually.
  • Batch Reordering: If you need to rearrange multiple items at once, fetch all items with form.getItems(), then loop through them and set their indices in your desired order.

内容的提问来源于stack exchange,提问作者Matthew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:11:05