Raffle Sheet to Capture 3 Entries for 3 Ticket Option is NOT Working

I have a Google form where I’m attempting to run a charity raffle. One of the questions is a multiple choice: 1 Raffle Ticket – $20 and the other option is: 3 Raffle Tickets – $50. If someone selects “3 Raffle Tickets” and submits the form, I need 3 corresponding entries in my Google Sheet for the number of tickets they purchased.

I tried using AI to create the Apps Script, but have been unsuccessful. Here is what I currently have:

function onFormSubmit(e) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lastRow = sheet.getLastRow();
  var values = sheet.getRange(lastRow, 1, 1, sheet.getLastColumn()).getValues()[0];
  
  var ticketChoice = values[4]; // Assuming ticket choice is in column E
  
  if (ticketChoice.includes("3 Raffle Tickets")) {
    // Add two more rows for 3 Raffle Tickets
    for (var i = 0; i < 2; i++) {
      sheet.appendRow(values);
    }
  }
  // Do nothing for 1 Raffle Ticket as it's already added by form submission
}

I also added a Trigger for this Apps Script:

  • Choose which function to run: onFormSubmit
  • Choose which deployment should run: Head
  • Select event source: From spreadsheet
  • Select event type: On form submit

In testing the form, here is what’s happening. When I select the “3 Raffle Tickets” option, it appears to work:
Test with 3 Raffle Tickets option

However, in my next test I select the “1 Raffle Ticket” option, and here’s what happens:
Test with 1 Raffle Ticket option

As you can see, the original entries for 3 tickets is there. However, when I submitted the 1 Rafflet Ticket option, it was added, but 2 other entries were added for the original 3 Ticket entry. Any help in resolving this would be greatly appreciated so we can launch this charity raffle on time. Thank you!

I’m not a scripting expert, so I tried using Perplexity AI to get a working script (see above), but it’s been unsuccessful.

I’m expecting that when someone buys a “3 Raffle Tickets” option, 3 entries are added to the Google Sheet and when someone buys a “1 Raffle Ticket” option, 1 entry is added to the sheet. People can have multiple entries.

UPDATE: I have the script working now and wanted to share it so anyone could benefit from it.

function onFormSubmit(e) {
  try {
    var sheet = e.range.getSheet();
    var ticketColumnIndex = 5;  // Column E = 5
    
    var ticketOption = e.values[ticketColumnIndex - 1].trim();
    Logger.log("Value retrieved from column: " + ticketOption);
    
    var numTickets = parseInt(ticketOption.match(/d+/)[0], 10);
    Logger.log("Number of tickets: " + numTickets);
    
    var lastRow = sheet.getLastRow();
    var rowData = e.values;
    
    // Always add the original entry to the end
    sheet.getRange(lastRow + 1, 1, 1, rowData.length).setValues([rowData]);
    
    // Add additional entries for multiple tickets
    if (numTickets > 1) {
      for (var i = 1; i < numTickets; i++) {
        lastRow = sheet.getLastRow();
        sheet.getRange(lastRow + 1, 1, 1, rowData.length).setValues([rowData]);
      }
      Logger.log("Entry successfully duplicated " + (numTickets - 1) + " times and grouped at the end.");
    } else {
      Logger.log("Single ticket entry added at the end.");
    }
    
    // Remove the original entry added by the form
    sheet.deleteRow(e.range.getRow());
    
    // Remove any rows with "No Match" in column A
    var dataRange = sheet.getDataRange();
    var values = dataRange.getValues();
    for (var i = values.length - 1; i >= 0; i--) {
      if (values[i][0].toString().startsWith("No Match")) {
        sheet.deleteRow(i + 1);
      }
    }
    
  } catch (error) {
    Logger.log("Error during form submission processing: " + error);
  }
}

New contributor

Julio Ciamarra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

UPDATE: I have the script working now and wanted to share it so anyone could benefit from it.

function onFormSubmit(e) {
  processNewSubmissions();
}

function processNewSubmissions() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Form Responses 1');
  var data = sheet.getDataRange().getValues();
  var ticketColumnIndex = 5;  // Column E = 5
  var processedFlagIndex = 13;  // Column M = 13, we'll use this to mark processed entries
  var timestampIndex = 1;  // Assuming timestamp is in Column A = 1

  Logger.log("Processing submissions on sheet: " + sheet.getName());
  Logger.log("Total rows in sheet (including header): " + data.length);

  var newEntries = [];
  var rowToProcess = null;
  var latestTimestamp = new Date(0);

  // Find the most recent unprocessed submission
  for (var i = data.length - 1; i >= 1; i--) {  // Start from the bottom, skip header
    var timestamp = new Date(data[i][timestampIndex - 1]);
    var processedFlag = data[i][processedFlagIndex - 1];
    
    Logger.log("Row " + (i + 1) + ": Timestamp=" + timestamp + ", Processed Flag=" + processedFlag);
    
    if (processedFlag !== "Processed" && timestamp > latestTimestamp) {
      rowToProcess = i;
      latestTimestamp = timestamp;
    }
  }

  if (rowToProcess !== null) {
    var ticketOption = data[rowToProcess][ticketColumnIndex - 1].toString().trim();
    Logger.log("Processing submission at row " + (rowToProcess + 1) + ": " + ticketOption);

    var numTickets = 1;  // Default to 1 ticket
    var numMatch = ticketOption.match(/(d+)/);
    
    if (numMatch) {
      numTickets = parseInt(numMatch[1], 10);
    }
    
    Logger.log("Number of tickets to add: " + numTickets);

    // Add entries for this submission
    for (var j = 0; j < numTickets; j++) {
      var newEntry = data[rowToProcess].slice();  // Create a copy of the row
      if (j === 0) {
        newEntry[processedFlagIndex - 1] = "Processed";  // Mark the first entry as processed
      }
      newEntries.push(newEntry);
      Logger.log("Prepared entry " + (j + 1) + " of " + numTickets + " for " + ticketOption);
    }

    // Add all new entries at once, keeping them grouped
    sheet.getRange(sheet.getLastRow() + 1, 1, newEntries.length, newEntries[0].length).setValues(newEntries);
    Logger.log("Added " + newEntries.length + " new entries to the sheet.");

    // Delete the original unprocessed row
    sheet.deleteRow(rowToProcess + 1);
    Logger.log("Deleted original unprocessed row at position " + (rowToProcess + 1));
  } else {
    Logger.log("No new unprocessed submissions found.");
  }

  // Final check
  var finalRowCount = sheet.getLastRow() - 1;  // Subtract 1 for header row
  Logger.log("Final row count (excluding header): " + finalRowCount);
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Raffle Sheet to Capture 3 Entries for 3 Ticket Option is NOT Working

I have a Google form where I’m attempting to run a charity raffle. One of the questions is a multiple choice: 1 Raffle Ticket – $20 and the other option is: 3 Raffle Tickets – $50. If someone selects “3 Raffle Tickets” and submits the form, I need 3 corresponding entries in my Google Sheet for the number of tickets they purchased.

I tried using AI to create the Apps Script, but have been unsuccessful. Here is what I currently have:

function onFormSubmit(e) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lastRow = sheet.getLastRow();
  var values = sheet.getRange(lastRow, 1, 1, sheet.getLastColumn()).getValues()[0];
  
  var ticketChoice = values[4]; // Assuming ticket choice is in column E
  
  if (ticketChoice.includes("3 Raffle Tickets")) {
    // Add two more rows for 3 Raffle Tickets
    for (var i = 0; i < 2; i++) {
      sheet.appendRow(values);
    }
  }
  // Do nothing for 1 Raffle Ticket as it's already added by form submission
}

I also added a Trigger for this Apps Script:

  • Choose which function to run: onFormSubmit
  • Choose which deployment should run: Head
  • Select event source: From spreadsheet
  • Select event type: On form submit

In testing the form, here is what’s happening. When I select the “3 Raffle Tickets” option, it appears to work:
Test with 3 Raffle Tickets option

However, in my next test I select the “1 Raffle Ticket” option, and here’s what happens:
Test with 1 Raffle Ticket option

As you can see, the original entries for 3 tickets is there. However, when I submitted the 1 Rafflet Ticket option, it was added, but 2 other entries were added for the original 3 Ticket entry. Any help in resolving this would be greatly appreciated so we can launch this charity raffle on time. Thank you!

I’m not a scripting expert, so I tried using Perplexity AI to get a working script (see above), but it’s been unsuccessful.

I’m expecting that when someone buys a “3 Raffle Tickets” option, 3 entries are added to the Google Sheet and when someone buys a “1 Raffle Ticket” option, 1 entry is added to the sheet. People can have multiple entries.

UPDATE: I have the script working now and wanted to share it so anyone could benefit from it.

function onFormSubmit(e) {
  try {
    var sheet = e.range.getSheet();
    var ticketColumnIndex = 5;  // Column E = 5
    
    var ticketOption = e.values[ticketColumnIndex - 1].trim();
    Logger.log("Value retrieved from column: " + ticketOption);
    
    var numTickets = parseInt(ticketOption.match(/d+/)[0], 10);
    Logger.log("Number of tickets: " + numTickets);
    
    var lastRow = sheet.getLastRow();
    var rowData = e.values;
    
    // Always add the original entry to the end
    sheet.getRange(lastRow + 1, 1, 1, rowData.length).setValues([rowData]);
    
    // Add additional entries for multiple tickets
    if (numTickets > 1) {
      for (var i = 1; i < numTickets; i++) {
        lastRow = sheet.getLastRow();
        sheet.getRange(lastRow + 1, 1, 1, rowData.length).setValues([rowData]);
      }
      Logger.log("Entry successfully duplicated " + (numTickets - 1) + " times and grouped at the end.");
    } else {
      Logger.log("Single ticket entry added at the end.");
    }
    
    // Remove the original entry added by the form
    sheet.deleteRow(e.range.getRow());
    
    // Remove any rows with "No Match" in column A
    var dataRange = sheet.getDataRange();
    var values = dataRange.getValues();
    for (var i = values.length - 1; i >= 0; i--) {
      if (values[i][0].toString().startsWith("No Match")) {
        sheet.deleteRow(i + 1);
      }
    }
    
  } catch (error) {
    Logger.log("Error during form submission processing: " + error);
  }
}

New contributor

Julio Ciamarra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

UPDATE: I have the script working now and wanted to share it so anyone could benefit from it.

function onFormSubmit(e) {
  processNewSubmissions();
}

function processNewSubmissions() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Form Responses 1');
  var data = sheet.getDataRange().getValues();
  var ticketColumnIndex = 5;  // Column E = 5
  var processedFlagIndex = 13;  // Column M = 13, we'll use this to mark processed entries
  var timestampIndex = 1;  // Assuming timestamp is in Column A = 1

  Logger.log("Processing submissions on sheet: " + sheet.getName());
  Logger.log("Total rows in sheet (including header): " + data.length);

  var newEntries = [];
  var rowToProcess = null;
  var latestTimestamp = new Date(0);

  // Find the most recent unprocessed submission
  for (var i = data.length - 1; i >= 1; i--) {  // Start from the bottom, skip header
    var timestamp = new Date(data[i][timestampIndex - 1]);
    var processedFlag = data[i][processedFlagIndex - 1];
    
    Logger.log("Row " + (i + 1) + ": Timestamp=" + timestamp + ", Processed Flag=" + processedFlag);
    
    if (processedFlag !== "Processed" && timestamp > latestTimestamp) {
      rowToProcess = i;
      latestTimestamp = timestamp;
    }
  }

  if (rowToProcess !== null) {
    var ticketOption = data[rowToProcess][ticketColumnIndex - 1].toString().trim();
    Logger.log("Processing submission at row " + (rowToProcess + 1) + ": " + ticketOption);

    var numTickets = 1;  // Default to 1 ticket
    var numMatch = ticketOption.match(/(d+)/);
    
    if (numMatch) {
      numTickets = parseInt(numMatch[1], 10);
    }
    
    Logger.log("Number of tickets to add: " + numTickets);

    // Add entries for this submission
    for (var j = 0; j < numTickets; j++) {
      var newEntry = data[rowToProcess].slice();  // Create a copy of the row
      if (j === 0) {
        newEntry[processedFlagIndex - 1] = "Processed";  // Mark the first entry as processed
      }
      newEntries.push(newEntry);
      Logger.log("Prepared entry " + (j + 1) + " of " + numTickets + " for " + ticketOption);
    }

    // Add all new entries at once, keeping them grouped
    sheet.getRange(sheet.getLastRow() + 1, 1, newEntries.length, newEntries[0].length).setValues(newEntries);
    Logger.log("Added " + newEntries.length + " new entries to the sheet.");

    // Delete the original unprocessed row
    sheet.deleteRow(rowToProcess + 1);
    Logger.log("Deleted original unprocessed row at position " + (rowToProcess + 1));
  } else {
    Logger.log("No new unprocessed submissions found.");
  }

  // Final check
  var finalRowCount = sheet.getLastRow() - 1;  // Subtract 1 for header row
  Logger.log("Final row count (excluding header): " + finalRowCount);
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật