简体   繁体   中英

What is code to count last row and add cell data to new blank row

The code copies data from cell M4 and to cell L45, which is good but if I change the date in M4 and run the script it will overwrite cell L45 with the new info instead of copying to the next blank row below (cell L46) What is the adjustment needed and where would it go in the script

Here is code:

function moveValuesOnly() {

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var source = ss.getRange('M4');
 source.copyTo(ss.getRange('L45'), {contentsOnly: true}); 

This function will append any changes in 'M4' to the bottom of column L.

function onEdit(e) {
  var sh=e.range.getSheet();
  if(sh.getName()!='Sheet1') {return;};//edit Sheet name for your requirements
  if(e.range.getA1Notation()=='M4') {
    e.range.getSheet().getRange(getColumnHeight(12,sh,e.source) + 1,12).setValue(e.value);
  }
}

function getColumnHeight(col,sh,ss){
  var ss=ss || SpreadsheetApp.getActive();
  var sh=sh || ss.getActiveSheet();
  var col=col || sh.getActiveCell().getColumn();
  var rg=sh.getRange(1,col,sh.getLastRow(),1);
  var vA=rg.getValues();
  while(vA[vA.length-1][0].length==0){
    vA.splice(vA.length-1,1);
  }
  return vA.length;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM