Search code examples
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:

    • In your showing script, it seems that the destination sheet is different from the sheet obtained in the last row. I guess that this might be the reason for your current issue. In this case, please retrieve the last row from the destination sheet.
    • When appendRow is used, getLastRow() is not required to be used.
    • In your script, for example, recordsSht.getRange(blankRow,3).setValue(form.getRange("B12:B25").getValue()); is used. In this case, the 1st cell of "B12:B25" is used. What is your expected result? If you want to use the values of "B12:B25", it is required to modify the script from getValue to getValues.

    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);
    }
    
    • When this script is run, the values of "D8" and "B8" are appended into columns "A" and "B" of "Billing Masterlist" sheet. And, values of rows 12 to 25 of columns "B" to "C" are appended into columns "C" and "D" of "Billing Masterlist" sheet. And, values of rows 12 to 25 of columns "E" to "H" are appended into columns "E" to "H" of "Billing Masterlist" sheet.

    • I'm not sure what you want to do at recordsSht.getRange(blankRow,3).setValue(form.getRange("B12:B25").getValue()). So, in this modification, the values of rows from 12 to 25 are converted to a string using join. If you want another result, please tell me.

    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);
    }
    
    • I reflected your reply of the issue now is it pulls everything including the empty row from the data entry form inside the billing master list instead of only a specific row.