Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save user52839307/ec77b904e1f459ec31ff39a81f3ea82d to your computer and use it in GitHub Desktop.
Save user52839307/ec77b904e1f459ec31ff39a81f3ea82d to your computer and use it in GitHub Desktop.
To add existing rows from a Google Sheets document as responses to a Google Form, you'll need a workaround, as Google Forms doesn't natively support importing data directly into its response system. However, you can achieve this by using Google Apps Script to programmatically submit form responses. This process involves reading the rows from you…
// If your active sheet is not the same as the form respons sheet and does not have a Timestamp Column.
function submitFormResponses() {
var form = FormApp.openById('YOUR_FORM_ID'); // Replace with your Form ID
var spreadsheet = SpreadsheetApp.openById('YOUR_SPREADSHEET_ID'); // Replace with your Spreadsheet ID
var sheet = spreadsheet.getSheetByName('YOUR_SHEET_NAME'); // Replace with your Sheet name
// var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues(); // Adjust this if your data starts from a different row/column
var items = form.getItems();
data.forEach(function(row, index) {
// Skip header row or any rows you don't want to process
if (index > 0) {
var formResponse = form.createResponse();
for (var i = 0; i < items.length; i++) {
var item = items[i];
var itemType = item.getType();
var response = row[i]; // Adjust index based on your data layout
// Create response based on item type
switch(itemType) {
case FormApp.ItemType.TEXT:
var textItem = item.asTextItem();
var textResponse = textItem.createResponse(response);
formResponse.withItemResponse(textResponse);
break;
// Add cases for other item types as needed
}
}
// Submit the form response
formResponse.submit();
}
});
}
// If your active sheet is the same as the form respons sheet and has a Timestamp Column.
function submitFormResponses() {
var form = FormApp.openById('YOUR_FORM_ID'); // Replace with your Form ID
var spreadsheet = SpreadsheetApp.openById('YOUR_SPREADSHEET_ID'); // Replace with your Spreadsheet ID
var sheet = spreadsheet.getSheetByName('YOUR_SHEET_NAME'); // Replace with your Sheet name
// var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues(); // Adjust this if your data starts from a different row/column
var items = form.getItems();
data.forEach(function(row, index) {
// Skip header row or any rows you don't want to process
if (index > 0) {
var formResponse = form.createResponse();
for (var i = 0; i < items.length; i++) { // Start iterating over items from 0
var item = items[i];
var itemType = item.getType();
var response = row[i + 1]; // Adjust to start from 1 to skip timestamp column
// Create response based on item type
switch(itemType) {
case FormApp.ItemType.TEXT:
var textItem = item.asTextItem();
var textResponse = textItem.createResponse(response);
formResponse.withItemResponse(textResponse);
break;
// Add cases for other item types as needed
}
}
// Submit the form response
formResponse.submit();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment