// Copyright © 2020 Dimakakos Dimakis // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see // . function AppendResponses() { // get spreadsheet data const sheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1JV3BUiZ1t2N1NiX-yW-XMxEgo-qOVVSifjFVQVMe24E/edit"); const rows = sheet.getDataRange(); const numRows = rows.getNumRows(); const values = rows.getValues(); // get form data const form = FormApp.openByUrl("https://docs.google.com/forms/d/1IVrUpV_cIljb-EzN2_JUMXlTo-UozDQPOpRz_dh7m_s/edit"); const items = form.getItems().filter(i => i.getType() != "SECTION_HEADER"); // filter out section headers, since these get no answers // for every row in values ... for (let v = 1; v < values.length; v ++) { const value = values[v]; const formResponse = form.createResponse(); // the response object we will submit let data_index = 1; // we will increment on this, but we don't want the first column, since it's the date! // ... take each element and make it a response for the corresponding item of the form // CHECKBOX answers are in a single cell as comma separated values // GRID answers take multiple cells, each for every row, so we need to keep track of the column position for (let i = 0; i < items.length; i++) { const item = items[i]; const type = item.getType(); if (type == "TEXT") { const textItem = item.asTextItem(); const response = textItem.createResponse(value[data_index]); formResponse.withItemResponse(response); data_index++; } else if (type == "PARAGRAPH_TEXT") { const paragraphTextItem = item.asParagraphTextItem(); const response = paragraphTextItem.createResponse(value[data_index]); formResponse.withItemResponse(response); data_index++; } else if (type == "MULTIPLE_CHOICE") { const multipleChoiceItem = item.asMultipleChoiceItem(); const response = multipleChoiceItem.createResponse(value[data_index]); formResponse.withItemResponse(response); data_index++; } else if (type == "CHECKBOX") { const arr = value[data_index].split(',').map(x => x.trim()); // since we split on commas, it's thoughtfull to replace commas in user submitted answers with dashes const checkboxItem = item.asCheckboxItem(); const response = checkboxItem.createResponse(arr); formResponse.withItemResponse(response); data_index++; } else if (type == "GRID") { const gridItem = item.asGridItem(); const gridHeight = gridItem.getRows().length; const arr = value.slice(data_index, data_index + gridHeight); const response = gridItem.createResponse(arr); formResponse.withItemResponse(response); data_index += gridHeight; } else { throw new Error('This kind of item in not implemented. DIY or drop a line at me@bendersteed.tech'); } } formResponse.submit(); Utilities.sleep(500); } };