google-sheetsdata-entrymultirow

Add multirow of data from data entry to database


Currently, i'm working on a dashboard but i couldn't find any solution to solve a issue of my code where the problem is when i click the submit button, only the top row is recorded or save inside the Database. Below is an example (with image) of what i'm trying to achieve to this dashboard

Step 1: Submit Data from Data Entry Submission From Data Entry

Step 2: Recorded in Database (Things i want to achieve) Recorded Data

The issue i face: Issue only one data is recorded

Here is the code im currently working on right now

function Submit() {

  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var form= ss.getSheetByName('Data Entry Form');
  var recordsSht=ss.getSheetByName('Billing Masterlist');

  var blankRow=datasheet.getLastRow()+1; //identify the next blank row

  recordsSht.getRange(blankRow,1).setValue(form.getRange("D8").getValue());
  recordsSht.getRange(blankRow,2).setValue(form.getRange("B8").getValue());
  recordsSht.getRange(blankRow,3).setValue(form.getRange("B12:B25").getValue());
  recordsSht.getRange(blankRow,4).setValue(form.getRange("C12:C25").getValue());
  recordsSht.getRange(blankRow,5).setValue(form.getRange("E12:E25").getValue());
  recordsSht.getRange(blankRow,6).setValue(form.getRange("F12:F25").getValue());
  recordsSht.getRange(blankRow,7).setValue(form.getRange("G12:F25").getValue());
  recordsSht.getRange(blankRow,8).setValue(form.getRange("H12:H25").getValue());
  
}


Solution

  • Modification points:

    When these points are reflected in your script, how about the following modification?

    Modified script:

    function Submit() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var form = ss.getSheetByName('Data Entry Form');
      var recordsSht = ss.getSheetByName('Billing Masterlist');
      var convert = array => array[0].map((_, col) => array.map((row) => row[col] || "")).map(c => c.filter(String).join(", "));
      var values = [
        form.getRange("D8").getValue(),
        form.getRange("B8").getValue(),
        ...convert(form.getRange("B12:C25").getValues()), // or ...convert(form.getRange("B12:C25").getDisplayValues()), 
        ...convert(form.getRange("E12:H25").getValues()), // or ...convert(form.getRange("E12:H25").getDisplayValues()),
      ];
      recordsSht.appendRow(values);
    }
    

    References:

    Added:

    About your following reply,

    I want the result to be in multiple rows instead of a single row in the "Billing Masterlist" after submission from the data entry. For more clarification, can refer to Step 2 on what exactly I want it to be Here is the testing sheet for your reference

    If my understanding of your following reply is correct, how about the following sample script?

    function Submit() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var form = ss.getSheetByName('Data Entry Form');
      var recordsSht = ss.getSheetByName('Billing Masterlist');
      var [[b8, , d8]] = form.getRange("B8:D8").getValues();
      var b12h25 = form.getRange("B12:H25").getValues();
      var values = b12h25.reduce((ar, r) => {
        const [b, c, , ...efgh] = r;
        ar.push([d8, b8, b, c, ...efgh]);
        return ar;
      }, []);
      if (values.length == 0) return;
      recordsSht.getRange(recordsSht.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
    }