Google-sheets – Button in column C in each row

google sheetsgoogle-apps-script

I have a spreadsheet with three columns and lots of rows.

Column A has a numerical ID
Column B is empty
Column C is empty

I want to have a button in each row in column C. When pressed, it should take the information in column A of the same row and do some processing on it and output the result in column B.
The processing will make use of outside servers to do some data fetching, hence I cannot do this on sheet load because of "data overload". (actually I tried first, but it takes ages, times out etc with many rows).
So I want to have it load that information adhoc, on the press of a button on the same line.

I have looked around and it seems that the only way to make buttons is to make a drawing of a button and then tie that button to a script. The problem with this approach is that the buttons aren't tied to a a cell so I can't get the current row number where the button is, to do processing on only that row.

Any fix?

Best Answer

Best is to make a custom menu, via Google Apps Script. This will allow you activate things by row.

Code

function onOpen() {
  var ss = SpreadsheetApp.getActive();
  var items = [{name: 'Magic', functionName: 'myMagic'}];
  ss.addMenu('Do Stuff', items);
}

function myMagic() {
  var ss = SpreadsheetApp.getActive(), aRow = ss.getActiveCell().getRow();
  var nDate = Utilities.formatDate(new Date(), "GMT-1", "dd-MM-yyyy");
  ss.getRange("B"+aRow).setValue(nDate);
}

Your magic needs to happen in the myMagic function.

Example

I've prepared an example file for you: button vs. menu
You can run the code and see it in the file itself.