Google 表单更新时使用脚本创建的表单新 Sheet 数据写入前一列的右侧

Google Form created with script when Form updated new Sheet data is written to right of prev columns

我有一个由 运行 脚本更新的表单。我需要动态更新下拉列表项。该脚本删除所有表单项,然后重新创建表单项。

我尝试使用此代码仅删除下拉列表项,但它删除了所有项目。因此,我尝试删除并重新创建所有表单项。我当前的脚本在 post.

中更靠下
    var items = form.getItems();
    for (var i in items) {
    if (Logger.log(items[i].getTitle() == 'For Customer (name) or Finished Goods')) {
         form.deleteItem(items[i]);
    // form.deleteItem(i); didn't delete either
    break;
    }
    }


The form works in that it saves data to the Sheet. The problem is when I rerun the script, and then submit another form, new columns are created in the Google Sheet to the right of the previous columns with the same column names (as the ones on the left) recreated too. See the attached Form and Sheet screenshot.

I think the problem may be that the items are recreated and may have a different item id ? It there a way to specify that the data is saved to the first left-most columns ?

My ultimate goal is to update the customer and product list dynamically - perhaps from a database, or from a Sheet that is updated via a URL CSV file (with the CSV generated hourly on the website via a cron job.

I did look before posting but didn't find an answer.
[enter image description here][1]
Here's my script. Maybe there's a better way. Your help would be greatly appreciated. Thank you.

     function updateForm() {
            var form = FormApp.openByUrl(
             'https://docs.google.com/forms/d/1-the-urlt'
             );
     FormApp.getUi()

    /* delete all items and recreate */
     var items = form.getItems();
    for (var i in items) {
        // Logger.log(items[i].getTitle() + ': ' + items[i].getId());
             form.deleteItem(items[i]);
    }



    form.addDateItem()
            .setTitle('Date Product was Manufactured');

    // now add a new customer list
    var ProductItem = form.addListItem();
     ProductItem.setTitle('Product')
             .setChoices([
                     ProductItem.createChoice('Aquacide'),
                     ProductItem.createChoice('Bromax'),
                     ProductItem.createChoice('Guardian'),
                     ProductItem.createChoice('Pro Guard'),
             ]);



     form.addTextItem()
        .setTitle('Batch Size (gal) made');

     form.addTextItem()
        .setTitle('Batch/Lot Number');

     form.addTextItem()
        .setTitle('Made By');



    var customerItem = form.addListItem();
     customerItem.setTitle('For Customer (name) or Finished Goods')
             .setChoices([
                     customerItem.createChoice('ABC Inc'),
                     customerItem.createChoice('Imx Corp'),
                     customerItem.createChoice('New Company'),
                     customerItem.createChoice('One More'),
                     customerItem.createChoice('Scream Inc'),
                     customerItem.createChoice('Z Tech')
             ]);
     form.addTextItem()
        .setTitle('Plant Operator pH Reading')
        .setHelpText('NOTE: Take reading when product is finished blending. Take reading of each Drum/Batch made');

     form.addTextItem()
        .setTitle('Tech Dir pH Reading')
        .setHelpText('NOTE: Must verify pH reading for 3022');

    Logger.log('Published URL: ' + form.getPublishedUrl());
    Logger.log('Editor URL: ' + form.getEditUrl());

    }


  [1]: https://i.stack.imgur.com/XZx27.png

======= 这是创建了 2 个下拉列表的工作代码 =====

function updateForm() {
    var form = FormApp.openByUrl(
     'https://docs.google.com/forms/d/1-yourURLhere/edit'
     );
 FormApp.getUi()

var items = form.getItems();
  for (var i in items) {
    var title = items[i].getTitle()

    if (title == 'Product') {
      var listItem = items[i].asListItem()
      var choices = []
      choices.push(listItem.createChoice("Aquacide"))
      choices.push(listItem.createChoice("Bromax"))
      choices.push(listItem.createChoice("Guardian"))
      choices.push(listItem.createChoice("Z-Blend"))
      listItem.setChoices(choices)


  }  // product
  else if (title == 'For Customer (name) or Finished Goods') {
      var custItem = items[i].asListItem()
      var custChoices = []
      custChoices.push(custItem.createChoice("ABC Inc"))
      custChoices.push(custItem.createChoice("BMC Corp"))
      custChoices.push(custItem.createChoice("IDX Inc"))
      custChoices.push(custItem.createChoice("Z-Test Inc"))
      custItem.setChoices(custChoices)

  }  // Customer
}    // for loop



Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Editor URL: ' + form.getEditUrl());

}

您删除所有项目的原因是因为以下语句

if (Logger.log(items[i].getTitle() == 'For Customer (name) or Finished Goods')) 

应该是

Logger.log(items[i].getTitle() == 'For Customer (name) or Finished Goods')
if (items[i].getTitle() == 'For Customer (name) or Finished Goods')

Logger.log 函数 returns 返回一个 none 空对象,它总是被 if 语句认为是真的。因此,if语句总是执行,删除所有表单元素

此外,您可以按如下所述添加新选项,而不是删除选项。旧的将被替换,因为你只是更新你的旧项目(没有新的项目 ID),你不会得到一个新的列。

更新选择的最终代码:

var items = form.getItems();
  for (var i in items) {
   if (items[i].getTitle() == 'For Customer (name) or Finished Goods') {
      var listItem = items[i].asListItem()
      var choices = []
      choices.push(listItem.createChoice("Option1"))
      choices.push(listItem.createChoice("Option2"))
      choices.push(listItem.createChoice("Option3"))
      listItem.setChoices(choices)


   break;
  }
}