const templateSheet = getSheetByName("MyTemplate");
const formResponsesSheet = getSheetByName("ADD TILE FORM");
const archivedColumnLabel = "ARCHIVED";
const archivedColumnValue = "ARCHIVED";
function getSheetByName(sheetName) {
return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
}
function getRowRange(sheet,row) {
return sheet.getRange(row,1,1,sheet.getLastColumn());
}
function getColumnRange(sheet,column) { // omits header
return sheet.getRange(2,column,sheet.getLastRow(),1);
}
function getCellRange(sheet,row,column) {
return sheet.getRange(row,column,1,1);
}
// -- Adding and removing form questions --
// Custom headers should always be added from the Form Responses sheet and not to the template sheet directly
// Adding a question to the form would require subsequently inserting a column into the template sheet directly after the column for the last question on the form.
// Deleting a question from the form would require subsequently deleting the blank column that appears directly after the column for the last question on the form.
// -- Adding and removing custom fields --
// All custom fields are tracked by their name. This means they can freely be added or removed from the FORM RESPONSES sheet as necessary. Just remember to shift over the custom field formulas such that they continue to align with their corresponding custom field header label.
function addTemplateFormulasToFormResponseSheet(templateSheet,formResponsesSheet,rowIndexStart,totalNewRows) {
const templateSheetHeaderRange = getRowRange(templateSheet,1);
const formResponsesSheetHeaderRange = getRowRange(formResponsesSheet,1);
const formResponsesSheetHeaderValues = formResponsesSheetHeaderRange.getValues().flat();
const templateSheetHeaderValues = templateSheetHeaderRange.getValues().flat();
const templateSheetFormulaRowIndex = 2;
const templateSheetFormulaCells = Array.from({length:templateSheetHeaderValues.length}).map(
(v,i)=>getCellRange(templateSheet,templateSheetFormulaRowIndex,i+1)
);
const sortedTemplateSheetFormulaCells = formResponsesSheetHeaderValues.map(formResponsesHeaderValue=>{
const headerIndex = templateSheetHeaderValues.indexOf(formResponsesHeaderValue);
if (headerIndex==-1) { return ""; } else { return templateSheetFormulaCells[headerIndex] };
});
sortedTemplateSheetFormulaCells.forEach((sortedTemplateSheetFormulaCell,i)=>{
if (sortedTemplateSheetFormulaCell.getFormula() == "") { return; }
const formResponsesSheetNewRowCell = getCellRange(formResponsesSheet,rowIndexStart,i+1);
if (formResponsesSheetNewRowCell.getValue() != "") { return; }
sortedTemplateSheetFormulaCell.copyTo(formResponsesSheetNewRowCell);
formResponsesSheetNewRowCell.setValue(formResponsesSheetNewRowCell.getValue());
});
}
function receiverOfFormSubmission(e) {
const newRowIndexStart = e.range.rowStart;
const newRowIndexEnd = e.range.rowEnd;
const totalNewRows = 1 + (newRowIndexEnd-newRowIndexStart);
addTemplateFormulasToFormResponseSheet(templateSheet,formResponsesSheet,newRowIndexStart,totalNewRows);
}
function archiveEveryRow() {
const headersArray = getRowRange(formResponsesSheet,1).getValues().flat();
const archivedColumnIndex = headersArray.indexOf(archivedColumnLabel)+1;
if (archivedColumnIndex == 0) { return; }
const archivedColumnValues = getColumnRange(formResponsesSheet,archivedColumnIndex).getValues().flat();
archivedColumnValues.forEach((v,i)=>{
if (v != archivedColumnValue) { getCellRange(formResponsesSheet,i+1,archivedColumnIndex).setValue(archivedColumnValue); }
});
}
function onOpen() {
SpreadsheetApp.getUi().createMenu('My Menu')
.addItem('Archive every row', 'archiveEveryRow')
.addToUi()
}