Is there some approaches that will log information in the last row for specific columns?

谁说胖子不能爱 提交于 2021-01-29 14:12:59

问题


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).


回答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);


}


来源:https://stackoverflow.com/questions/64766282/is-there-some-approaches-that-will-log-information-in-the-last-row-for-specific

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!