Google 脚本,用于从单独的表单更新 Google 表单的多项选择问题的选择。除一种情况外都有效

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

这项工作使用 Google 表单进行输入,使用响应表来收集用户响应数据和触发代码,以及一个单独的工作簿来推送多项选择题的答案选择。

用户填写 Google 表单,从面对面小组会议列表中选择日期,或从远程小组会议列表中选择日期,每个日期都是一个单独的多项选择问题。每种类型的约会(无论是面对面还是远程)都会在 Google 表格工作簿中的单独工作表选项卡上列出日期和时间,并在相邻单元格中显示一个数字,说明该约会时间有多少个座位。 Google Apps 脚本会在表单提交时触发,并从适当的预约类型中减去席位。当座位号减至 0 时,该预约不再作为预约表上的选项出现。

这每次都很完美,除非连续选择两个相同类型和时间的约会。在这种情况下,尽管座位在 Google 表格上显示为 0,但 Google 表单仍包含此预约时间作为选择,直到下次提交表单为止。因此,可用座位为 -1。它不会再次被列为选择。如果选择的预约时间不是连续相同的,座位数不会达到-1,并且预约不会超额预订。

当更改、打开或提交表单时,表单响应电子表格上的 Google Apps 脚本会触发。

我向工作簿添加了一个带有日期的类似脚本,这样当编辑它时,它会将更改推送到表单,并且当在编辑器中打开表单时,它也将从工作簿中更新。添加这些是为了尝试解决该问题。他们帮助隔离了连续场景中两个选择的实例。它工作得很好,可以让它成为现实,但它让我发疯,因为我不明白为什么它会这样做。

由于需要单独的文件,外人很难复制。所以,我希望有人可以通过代码本身来判断,特别是因为它大部分时间都有效。

这是代码


function autodates(e) {
  
// Get a reference to the spreadsheet "Placement Test: User Editable Appointment Form (Responses)"
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

//Get a reference to the Form 
  const form =FormApp.openById("1XfvnBAnd1-fqgd5kCycv8fUToCTnLPeysb9Niw_j7bk")
  
//For the purpose of writing this program, the form's question ID's are necessary. Use the following script to get the unique item ID for the questions of the form by logging them to this and selecting run while you reproduce this program for other forms. When you run this, you will be required to give permissions! Collect the Id's for the form questions that you will update with the dates listed on a separate form so that you can identify the question to edit for your form.
  var questions = form.getItems();
  questions.forEach(question => {
    Logger.log(question.getTitle())
    Logger.log(question.getType())
    Logger.log(question.getId().toString())
  })
  
//First date list question retrieved by the logger.logs above:
  const dateQuestion = form.getItemById("1598842866");
 
//Second date list question retrieved by the logger.logs above:
  const dateQuestion2 = form.getItemById("713650670");
  
//UPDATING DATES
    //Get a reference to the workbook you want to access that has the list of dates and how many seats available per date
    
const dateListWB = SpreadsheetApp.openById("1EnNmU4Ii5BVruFOnlMF8pa7a7EoASZ33CweeR8_vdmg");
    
//First Date Question
//Get a reference to the correct tab of the workbook associated with the corresponding dates for the question. In this case, its the first tab:
    
const iPsheet = dateListWB.getSheetByName("In Person Appt Dates");
    
//and specify the range you want to collect. Since the dates are in column A, and number of seats in B:
    const iPsheetArray = iPsheet.getRange("A3:B").getValues();
    
//but you don't want to get the rows with empty spaces:
    const filterediPsheetArray = iPsheetArray.filter(iProw=>iProw[0]);
    
//Limit the choices so that if the value of column B, the number of seats, falls below 1, it can no longer be used
    const limitchoice = filterediPsheetArray.filter(function (iProw) {
      return iProw[1] >= 1;
    });
    Logger.log('limitchoice');
    Logger.log(limitchoice);
    
//The second number in the splice below indicates THE NUMBER OF DATE CHOICES that will show on the form for this question.
    if (limitchoice.length > 0) {
      var datecut = limitchoice.splice(0,5).map(function(row) {
        return row [0]
      });
      Logger.log('datecut');
      Logger.log(datecut)
    } 
    if (datecut.length > 0) {
      dateQuestion.asMultipleChoiceItem().setChoiceValues(datecut);
    } else {
      dateQuestion.asMultipleChoiceItem().setChoiceValues([]);
    };

//Second Date Question
    
//Get a reference to the correct tab of the workbook associated with the corresponding dates for the question. In this case, its the second tab:
    const remSheet =dateListWB.getSheetByName("Remote Appointment Dates");
    
//Get a reference to the Range you want to access from that sheet
    const remSheetArray = remSheet.getRange("A1:B").getValues();
    
//filter out any empty rows from the sheet
    const filteredremSheetArray = remSheetArray.filter(remrow => remrow[0]);
    
//Limit the choices so that if the value of columb B (the number of seats) falls below 1, it can no longer be used as a choice
    const limitchoice2 = filteredremSheetArray.filter(function(rem3row){
      return rem3row[1] >= 1;
    });
    Logger.log(limitchoice2);
    
//The second number in the splice below indicates the number of date choices that will be on for this question.
    if (limitchoice2.length > 0) {
      var datecut2 = limitchoice2.splice(0, 5).map(function(rem2row) {
        return rem2row[0];
      });
    }
    if (datecut2.length > 0) {
      dateQuestion2.asMultipleChoiceItem().setChoiceValues(datecut2);
      Logger.log(datecut2);
    } else {
      dateQuestion2.asMultipleChoiceItem().setChoiceValues([]);
    }

//UPDATING THE COUNT (or the number of seates for the date/time)
  
//First Date
//Get the date the student selected
  const rDateChoice = e.values[5]; //e.values are the numbered cells 
  counting the first column as [0]. Column B is [1] etc.
  const dateChoice = rDateChoice;
  
//Get the range of the data to search through
  const range = iPsheet.getRange("A:B");
  const data = range.getValues();
  
//Set the starting row index.
  let rowIndex = 1;
  
//Loop through the date choices until a blank row is encountered.
  while (rowIndex <= data.length && data[rowIndex][0] != "") {
    
//decrease value in column B by 1
    if (data[rowIndex][0] === dateChoice) {
      iPsheet.getRange(rowIndex +1, 2).setValue(data[rowIndex][1] - 1);
      break;
    }
    //move to the next row.
    rowIndex++;
  }
  
//Second Date
  const rDateChoice2 = e.values[6];
  const dateChoice2 = rDateChoice2;
  
//Get the range of the data to search through
  const range2 = remSheet.getRange("A:B");
  const data2 = range2.getValues();
  
//Set the starting row index.
  let rowIndex2 = 1;
  
//Loop through the date choices until a blank row is encountered.
  while (rowIndex2 <= data2.length && data2[rowIndex2][0] != "") {
    
//If the search string is found, decrease the value in column B by 1.
    if (data2[rowIndex2][0] === dateChoice2) {
      remSheet.getRange(rowIndex2 + 1, 2).setValue(data2[rowIndex2][1] - 1);
      break;
    }
    
//Move to the next row
    rowIndex2++;
  }
}
google-apps-script eventtrigger multiple-choice
1个回答
0
投票

您不能使用空列表,如

dateQuestion2.asMultipleChoiceItem().setChoiceValues([]);

您应该使用不可用的选项来绕过问题。如果这两个选项都消失了,请关闭表单。

© www.soinside.com 2019 - 2024. All rights reserved.