我可以让Google表单链接到Google表格吗?

时间:2019-10-27 21:46:17

标签: google-apps-script google-sheets google-form

我正在设置无声拍卖。由于该活动是为了慈善活动,因此我不愿意为任何软件付费,因此我尝试在Google中构建合适的软件。

在Google工作表的工作表“批次”中,我有一个范围为A1:F11的表,其中包括标题行。当我收到很多拍卖的捐赠时,此范围将有更多行。

我还设置了一个Google表单,我想链接到我在“手数”表上输入的数据。我想遍历Lots表的每一行,并将B列中的Lot名称添加到表单上的MultipleChoiceItem。

    function myFunction() {

    var form = FormApp.openByUrl(
     'FORM LINK'
     )
    var workbook  = SpreadsheetApp.openByUrl(
     'SHEET LINK')

    var sheet = book.getSheets()[0];

    itemArray = "VALUES FROM SHEET"

    var item = form.addMultipleChoiceItem();
    item.setTitle('Auction Lots')
    .setChoices([

    item.createChoice('ItemArray'),

     ])

     }

我已经编写了上面的代码,但实际上并不知道如何将工作表数据添加到数组中。除此之外,我想将C,D和E列的描述,底价和当前最高出价添加到项目列表中,但希望一旦我清除了第一个障碍,就不会有问题了!

我正在尝试的可能吗?

TIA!

1 个答案:

答案 0 :(得分:0)

我根据您对过程的描述创建了一个示例表格[1]和一个电子表格[2]。我开发了以下代码,以在用户提交表单时更新表格中的最高出价,前提是响应中的出价高于当前出价。为此,我使用了有关触发器[3],表单[4]和电子表格[5]服务的文档。

您需要运行一次createOnSubmitTrigger函数来创建触发器,该触发器将在用户发送表单响应时运行runOnFormSubmit函数:

function runOnFormSubmit(e) {
  //Get Form and Sheet objects
  var form = FormApp.openByUrl('Form-URL')
  var workbook  = SpreadsheetApp.openByUrl('Spreadsheet-URL')
  var sheet = workbook.getSheets()[0];

  //Get the user's responses for the multiple choice and bid price questions 
  var formResponse = e.response.getItemResponses();
  var lotsResponse = formResponse[0].getResponse();
  var bidPrice = formResponse[1].getResponse();

  //Use the selected lot name and find its row in the sheet
  var selectedLotName = lotsResponse.split(', ')[0];
  var textFinder = sheet.getRange(2, 2, sheet.getLastRow()-1, 1).createTextFinder(selectedLotName).matchCase(true).matchEntireCell(true);
  var highestBidCell = textFinder.findNext().offset(0, 3);

  //If the user's bid price is higher that the the current highest bid, update the sheets
  var highestBid = highestBidCell.getValue();
  if(bidPrice > highestBid) {
    highestBidCell.setValue(bidPrice);    


    //Get Sheets info in array and remove header
    var itemsArray = sheet.getDataRange().getValues();
    itemsArray.shift();

    //Get multiple choice question
    var lotsQuestion = formResponse[0].getItem().asMultipleChoiceItem();
    var choicesArray = [];

    //Build choices array from updated sheets info
    for(var i=0; i<itemsArray.length; i++) {
      var lotName = itemsArray[i][1];
      var description = itemsArray[i][2];
      var reservePrice = itemsArray[i][3];
      var highestBid = itemsArray[i][4];

      var choiceString = lotName + ', ' + description + ', ' + reservePrice + ', ' + highestBid;
      var choice = lotsQuestion.createChoice(choiceString);
      choicesArray.push(choice);
    }

    //Set choices array to the multiple choice question, this will only be done when the bid price is higher that the the current highest bid
    lotsQuestion.setChoices(choicesArray);
  }
}

//Creates on form submit trigger
function createOnSubmitTrigger() {
  var form = FormApp.openByUrl('Form-URL');
  ScriptApp.newTrigger('runOnFormSubmit')
  .forForm(form)
  .onFormSubmit()
  .create();
}

要确保脚本只能同时运行一次,可以使用锁定服务[6]。

[1] https://docs.google.com/forms/d/e/1FAIpQLSfP4skRoqQSY543wE7UbGMLMR7_glIvWpA-hl1k-kufudN64A/viewform?usp=sf_link

[2] https://docs.google.com/spreadsheets/d/1AaGeODbtm4vybQJqdoXL_3zB7hBwIYHZgcwTvtaaXN8/

[3] https://developers.google.com/apps-script/guides/triggers/events

[4] https://developers.google.com/apps-script/reference/forms/form-app

[5] https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

[6] https://developers.google.com/apps-script/reference/lock/lock