当表单更新新表单数据写入prev列右侧时,使用脚本创建的Google表单

时间:2017-05-12 15:26:08

标签: google-apps-script scripting google-sheets google-spreadsheet-api

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

我尝试使用此代码删除下拉列表项,但删除了所有项目。所以我尝试删除并重新创建所有表单项。我目前的剧本在这篇文章中更进一步。

    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());

}

1 个答案:

答案 0 :(得分:0)

您要删除所有项目的原因是由于以下声明

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函数返回一个非空对象,if语句始终认为该对象为true。因此,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;
  }
}