Home > Software design >  How to display info from other sheets, specific to text in cells
How to display info from other sheets, specific to text in cells

Time:04-25

Our company works at different properties repairing appliances, I would like to build a database to search up the information on each appliance at specific properties and in their specific apt/units, I created a form to start this process, but I need help with some complex coding.

I first created a box for the property, then I created an "Apt/Unit" box. The idea is when I select a property, the units tied to that property are shown in dropdown/type searchable list in the Apt/Unit box.

I then created an "Appliance type" box. The idea is when the "Apt/Unit" is selected, it will display the dropdown/type searchable list of the appliances tied to that specific "Apt/Unit".

Then I created boxes for the info for the appliance (Brand, Model #, Serial #, & Color), this is a bit more self-explanatory - once the appliance type is selected, it will display the respective information for each box for that appliance.

Here's the link to the Google sheet: enter image description here

enter image description here

Here is the sheet.

It works quite slow. I'd propose to use the native filters instead. Basically the script turns on and off the filters an changes data validation for the dropdown menus respectively.


Update

Here another version of the script. It works much faster but it uses the 'helper sheet' to store temporary data (the filtered table). You can hide the 'helper sheet' if you want.

// global variables
var SS = SpreadsheetApp.getActiveSpreadsheet();
var SHEET_USERFACE = SS.getSheetByName('Userface');
var SHEET_DATA = SS.getSheetByName('Data');
var SHEET_HELPER = SS.getSheetByName('Helper'); // the hidden sheet with temp data
var PROPERTY_LIST = [...new Set(SHEET_DATA.getRange('a2:a').getValues().flat())]; // 'Property' list
var DATA_OBJ = {}; 

function onl oad() { reset() }

function onEdit(e) {
  var {range, source, value} = e;

  if (range.getSheet().getName() != 'Userface') return;
  if (range.columnStart != 3) return;
  if (![9,11,13,15,17,19,21].includes(range.rowStart)) return;

  source.toast('Please, wait...');

  // reset whenever the first menu is changing
  if (range.rowStart == 9) {
    reset();
    source.getRange('c9').setValue(value);
  }

  var col_header = range.offset(0,-1).getValue();

  update_sheet_helper(col_header, value);
  update_all_dropdown_menus();
  
  source.toast('The sheet has been updated');
}

function reset() {
  SS.toast('Please wait...');

  // copy data from SHEET_DATA to SHEET_HELPER
  SHEET_USERFACE.getRange('c9:c21').clearContent().clearDataValidations();
  SHEET_DATA.getDataRange().copyTo(SHEET_HELPER.clearContents().getRange(1,1));

  update_data_obj();
  update_all_dropdown_menus();

  SS.toast('The sheet has been updated');
}

// make DATA_OBJECT from SHEET_HELPER
function update_data_obj() {
  DATA_OBJ = {};
  var [header, ...data] = SHEET_HELPER.getDataRange().getValues();
  for (let i in header) DATA_OBJ[header[i]] = data.map(e => e[i]);
  DATA_OBJ['Property'] = PROPERTY_LIST; // let 'Property' list will be full always
}

// remove from SHEET_DATA_HELPER all the rows
// that have no given value in column with given title
function update_sheet_helper(col_title, value) {
  var [header, ...data] = SHEET_HELPER.getDataRange().getValues();
  var col_index = header.indexOf(col_title);
  data = data.filter(k => k[col_index] == value);
  var table = [header, ...data];
  SHEET_HELPER.clearContents().getRange(1,1,table.length, table[0].length).setValues(table);
  update_data_obj();
}

function update_all_dropdown_menus() {
  SHEET_USERFACE.getRange('b9:c21').getValues().forEach((row,i) => {
      if (row[0] != '') set_data_validation(DATA_OBJ[row[0]], 'c'   (i 9));
    });

  function set_data_validation(data, cell_address) {
    var menu_list = [...new Set([...data])]; // remove duplicates from the array
    var menu_rule = SpreadsheetApp.newDataValidation().requireValueInList(menu_list).build();
    var cell_range = SHEET_USERFACE.getRange(cell_address)
    cell_range.setDataValidation(menu_rule);
    if (menu_list.length == 1) cell_range.setValue(menu_list[0]);
  }
}

The sheet is here.

  • Related