Created
October 26, 2021 07:16
-
-
Save Hoohm/ef201592e1a9dd35dd7dfdea533a60ba to your computer and use it in GitHub Desktop.
Code to manae a small spreadsheet form/database function. Explanation at https://www.youtube.com/watch?v=v2X-fArILPA&t=307s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Clear form | |
| function ClearCell(){ | |
| var ss = SpreadsheetApp.getActiveSpreadsheet() | |
| var formS = ss.getSheetByName("Form"); // Sheet name | |
| var rangesToClear = ["B3","B6","B8","B10","D6","D8","D10"]; // List of cells to clear | |
| for (var i=0; i<rangesToClear.length; i++){ | |
| formS.getRange(rangesToClear[i]).clearContent(); | |
| } | |
| } | |
| // Input Values | |
| function SubmitData(){ | |
| var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
| var formS = ss.getSheetByName("Form"); // Sheet with the form | |
| var dataS = ss.getSheetByName("Data"); // Sheet that will store the data | |
| var values = [[ | |
| formS.getRange("B6").getValue(), | |
| formS.getRange("B8").getValue(), | |
| formS.getRange("B10").getValue(), | |
| formS.getRange("D6").getValue(), | |
| formS.getRange("D8").getValue(), | |
| formS.getRange("D10").getValue()]]; | |
| dataS.getRange(dataS.getLastRow()+1, 1, 1, 6).setValues(values); // Write values to the data sheet | |
| ClearCell(); | |
| } | |
| var SEARCH_COL_IDX = 0; // Index number for search | |
| function Search(){ | |
| var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
| var formS = ss.getSheetByName("Form"); | |
| var str_idx = formS.getRange("B3").getValue(); | |
| var values = ss.getSheetByName("Data").getDataRange().getValues(); | |
| for (var i = 0; i < values.length; i++) { | |
| var row = values[i]; | |
| if (row[SEARCH_COL_IDX] == str_idx){ | |
| formS.getRange("B6").setValue(row[0]); | |
| formS.getRange("B8").setValue(row[1]); | |
| formS.getRange("B10").setValue(row[2]); | |
| formS.getRange("D6").setValue(row[3]); | |
| formS.getRange("D8").setValue(row[4]); | |
| formS.getRange("D10").setValue(row[5]); | |
| } | |
| } | |
| } | |
| function Update(){ | |
| var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
| var formS = ss.getSheetByName("Form"); // Sheet with the form | |
| var dataS = ss.getSheetByName("Data"); // Sheet that will store the data | |
| var str_idx = formS.getRange("B3").getValue(); | |
| var db_values = ss.getSheetByName("Data").getDataRange().getValues(); | |
| for (var i = 0; i < db_values.length; i++) { | |
| var row = db_values[i]; | |
| if (row[SEARCH_COL_IDX] == str_idx){ | |
| var INT_R = i+1 | |
| var form_values = [[ | |
| formS.getRange("B6").getValue(), | |
| formS.getRange("B8").getValue(), | |
| formS.getRange("B10").getValue(), | |
| formS.getRange("D6").getValue(), | |
| formS.getRange("D8").getValue(), | |
| formS.getRange("D10").getValue()]]; | |
| dataS.getRange(INT_R,1,1,6).setValues(form_values); | |
| SpreadsheetApp.getUi().alert('"Data Updated"') | |
| } | |
| } | |
| } | |
| function Delete(){ | |
| var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
| var formS = ss.getSheetByName("Form"); // Sheet with the form | |
| var dataS = ss.getSheetByName("Data"); // Sheet that will store the data | |
| var ui = SpreadsheetApp.getUi(); | |
| var response = ui.alert("Delete entry?", ui.ButtonSet.YES_NO); | |
| if (response == ui.Button.YES){ | |
| var str_idx = formS.getRange("B3").getValue(); | |
| var db_values = ss.getSheetByName("Data").getDataRange().getValues(); | |
| for (var i = 0; i < db_values.length; i++) { | |
| var row = db_values[i]; | |
| if (row[SEARCH_COL_IDX] == str_idx){ | |
| var INT_R = i+1 | |
| dataS.deleteRow(INT_R); | |
| ClearCell(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment