How do I add a new value to the end of a column in Google Sheets?

断了今生、忘了曾经 提交于 2020-06-16 05:42:21

问题


I'm trying to figure out how to add a value to the last row in a specific column in Google Sheets. Not all columns in the spreadsheet/table are of equal length so I can't simply look for the last row and add to a column. I need to find the next empty cell in a column and add the new value there. Looking through the docs I think involves the getRange() and setValue() functions but I think setting the actual range requires some additional coding to determine the next empty cell. Once I get that then I can do somtrhing like the following.

The button click triggers the addRecord() function

function addRecord(){

          var recVals = {};

          recVals.source = document.getElementById("source").value;
          recVals.medium = document.getElementById("medium").value;
          recVals.product = document.getElementById("product").value;

          google.script.run.userClicked(recVals);
}

Then in my apps scripts the userClicked() function runs and the value(s) are passed to it and written to the spreadsheet:

function userClicked(recVals){

  var ss = SpreadsheetApp.openById(ssId)
  var ws = ss.getSheetByName("Data");

  ws.appendRow([recVals.source, recVals.medium, recVals.product, new Date()]);
}

The problem is I am not adding a row and this is where I need help. Just as a disclaimer I'm not a programmer by trade but I've done fairly well researching and implementing solutions. I'm stuck on this one though.


回答1:


You can use the getNextDataCell() method of a range to get the first empty cell at the bottom of a particular column. The code below start the search at the bottom of the column and moves up through empty cells until it finds the first cell with a value. You need the first empty cell, so 1 needs to be added to the found value.

function userClicked(recVals){
  var columnLetterToGet,columnNumberToGet,direction,lastRow,lastRowInThisColWithData,
      rng,rowToSet,startOfSearch,valuesToSet;

  var ss = SpreadsheetApp.openById(ssId);//getActiveSpreadsheet();
  var ws = ss.getSheetByName("Data");

  lastRow = ws.getLastRow();
  //Logger.log('lastRow: ' + lastRow)

  columnNumberToGet = 3;//Edit this and enter the column number
  columnLetterToGet = "C";//Edit this and enter the column letter to get

  startOfSearch = columnLetterToGet + (lastRow + 2).toString();//Edit and replace with column letter to get
  //Logger.log('startOfSearch: ' + startOfSearch)

  rng = ws.getRange(startOfSearch);

  direction = rng.getNextDataCell(SpreadsheetApp.Direction.UP);//This starts
  //the search at the bottom of the sheet and goes up until it finds the
  //first cell with a value in it

  //Logger.log('Last Cell: ' + direction.getA1Notation())

  lastRowInThisColWithData = direction.getRow();
  //Logger.log('lastRowInThisColWithData: ' + lastRowInThisColWithData)

  rowToSet = lastRowInThisColWithData + 1;
  valuesToSet = [recVals.source, recVals.medium, recVals.product, new Date()];

  ws.getRange(rowToSet, 1,1,valuesToSet.length).setValues([valuesToSet]);
}

function test() {
  var o = {};

  o.source = "The source";
  o.medium = "The medium";
  o.product = "The product";

  userClicked(o);


}


来源:https://stackoverflow.com/questions/62393563/how-do-i-add-a-new-value-to-the-end-of-a-column-in-google-sheets

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