Gunga's Den
Gunga's Den

function addData() {

  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const dataEntryWS = ss.getSheetByName("Data entry")
  const dataTableWS = ss.getSheetByName("Data Table")
  const validationWS = ss.getSheetByName("Validation and Dollar amounts")

  //const idCell = validationWS.getRange("D6")
 
  const fieldRange = ["C4","C2","C6","D10", "D11",    "D12",    "D13",    "D14",    "D15",    "D16",    "D17",    "D18",    "D19",    "D20",    "D21",    "D22",    "D23",    "D24",    "D25",    "D26",    "D27",    "D28",    "D29",    "D30",    "D31",    "D32",    "D33",    "D34",    "D35",    "D36",    "D37",    "D38",    "D39",    "D40",    "D41",    "D42",    "D43",    "D44",    "D45",    "D46",    "D47",    "D48",    "D49",    "D50",    "D51",    "D52",    "D53",    "D54",    "D55",    "D56",    "D57",    "D58",    "E10", "E11",    "E12",    "E13",    "E14",    "E15",    "E16",    "E17",    "E18",    "E19",    "E20",    "E21",    "E22",    "E23",    "E24",    "E25",    "E26",    "E27",    "E28",    "E29",    "E30",    "E31",    "E32",    "E33",    "E34",    "E35",    "E36",    "E37",    "E38",    "E39",    "E40",    "E41",    "E42",    "E43",    "E44",    "E45"]

  const fieldValues = fieldRange.map(f => dataEntryWS.getRange(f).getValue())
  const nextIDcell = validationWS.getRange("A2")
  const nextID = nextIDcell.getValue()
  fieldValues.unshift(nextID)
  dataTableWS.appendRow(fieldValues)
  nextIDcell.setValue(nextID+1)

  dataTableWS.getRange("A1").activate();
  var currentCell = dataTableWS.getCurrentCell();
  dataTableWS.getActiveRange().getDataRegion().activate();
  currentCell.activateAsCurrentCell();
  dataTableWS.getActiveRangeList().setBorder(true, true, true, true, true, true, '#000000', SpreadsheetApp.BorderStyle.SOLID);
  dataTableWS.getRange("A1").activate();
 
  dataTableWS.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
  dataTableWS.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.PREVIOUS).activate();

  dataEntryWS.getRange("D10:E58").clearContent()  //Numbers
 

  var lastCellText
  var secondToLastText
  var sTLAddress

  dataTableWS.getRange('CL1').activate();
  dataTableWS.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
  lastCellText = dataTableWS.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).getValue()

  var activeCell = dataTableWS.getActiveCell();
  var row = activeCell.getRow();
  var newRow = row - 1

  sTLAddress = "CL" + newRow

  secondToLastText = dataTableWS.getRange(sTLAddress).getValue()

  if (lastCellText == secondToLastText) {
    SpreadsheetApp.getUi().alert("You may have dupe entries!!! \r\n Check the bottom two Rows in the Data Table \r\n \r\n If Dupes, delete the last row and decrease the 'Next Entry ID' in the 'Validation and Dollar amounts' Tab", SpreadsheetApp.getUi().ButtonSet.OK)
  }

  dataTableWS.getRange("A1").activate()  //Return to A1 on Data Table sheet

  var displayNr
  displayNr = validationWS.getRange('A5').getValue()  //need to adjust
  SpreadsheetApp.getUi().alert('Enter this number on the Inventory sheet: \r\n \r\n' + displayNr, SpreadsheetApp.getUi().ButtonSet.OK)


  dataEntryWS.getRange("A1").activate()  //Return to A1 on Data Entry sheet

}