0

In google sheets, I tried to log some information I entered in other sheet to my invoiceLogSheet. but the problem is : when the information log to the sheet, it always log in the last row for entire sheet, and what I want is : to log in last row for specific range of columns (clarify more: to check in specific columns their free last row and log the information on this row)

but I don't know how to script this .. this is the function I used and it does not do the job I want:

function InvoiceLog(invoice_number, part, due_date, totalInvoice, payable_to, project_name, note)
{
  
   //DEFINE ALL ACTIVE SHEETS
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  
  //DEFINE INVOICE LOG SHEET          
  var invoiceLogSheet = ss.getSheetByName("information"); 
  
  
  //GET LAST ROW OF INVOICE LOG SHEET
  var nextRowInvoice = invoiceLogSheet.getLastRow() + 1;
  
 
  
  //POPULATE INVOICE LOG
  invoiceLogSheet.getRange(nextRowInvoice, 2).setValue(invoice_number);
  invoiceLogSheet.getRange(nextRowInvoice, 3).setValue(part);  
  invoiceLogSheet.getRange(nextRowInvoice, 4).setValue(due_date);
  invoiceLogSheet.getRange(nextRowInvoice, 5).setValue(totalInvoice);
  invoiceLogSheet.getRange(nextRowInvoice, 6).setValue(payable_to);
  invoiceLogSheet.getRange(nextRowInvoice, 7).setValue(project_name);
  invoiceLogSheet.getRange(nextRowInvoice, 8).setValue(note);


}

the columns I want to check the last row in them is (B:H).

Marios
  • 26,333
  • 8
  • 32
  • 52
Hanin
  • 1

1 Answers1

1

Explanation:

One way to approach this problem is to use the accepted answer of this question.

Essentially for each of the selected columns you calculate the total number of rows like that:

invoiceLogSheet.getRange('B:B').getValues().filter(String).length +1

Please note that this approach will give you the total number of non empty cells in a particular column. If you have blank cells, then it will give you a smaller number than the correct one.

Solution:

function InvoiceLog(invoice_number, part, due_date, totalInvoice, payable_to, project_name, note)
{
  
   //DEFINE ALL ACTIVE SHEETS
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  
  //DEFINE INVOICE LOG SHEET          
  var invoiceLogSheet = ss.getSheetByName("information"); 
  
  
  //GET LAST ROW OF INVOICE LOG SHEET
  var nextRowInvoice = invoiceLogSheet.getLastRow() + 1;
  
  
  
  
  // new code
  //POPULATE INVOICE LOG
  invoiceLogSheet.getRange(invoiceLogSheet.getRange('B:B').getValues().filter(String).length +1, 2).setValue(invoice_number);
  invoiceLogSheet.getRange(invoiceLogSheet.getRange('C:C').getValues().filter(String).length +1, 3).setValue(part);  
  invoiceLogSheet.getRange(invoiceLogSheet.getRange('D:D').getValues().filter(String).length +1, 4).setValue(due_date);
  invoiceLogSheet.getRange(invoiceLogSheet.getRange('E:E').getValues().filter(String).length +1, 5).setValue(totalInvoice);
  invoiceLogSheet.getRange(invoiceLogSheet.getRange('F:F').getValues().filter(String).length +1, 6).setValue(payable_to);
  invoiceLogSheet.getRange(invoiceLogSheet.getRange('G:G').getValues().filter(String).length +1, 7).setValue(project_name);
  invoiceLogSheet.getRange(invoiceLogSheet.getRange('H:H').getValues().filter(String).length +1, 8).setValue(note);


}
Marios
  • 26,333
  • 8
  • 32
  • 52