0

I got this script working, but i want it to trigger, the moment i paste the values onto spreadsheet "raw Data", instead of manually running it.

function deleteColumns() {
  var sh1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("raw Data")
  sh1.deleteColumn(1)
  sh1.deleteColumn(3)
  sh1.deleteColumns(4,3)
  sh1.deleteColumns(8,7)

}
Dice 0ne
  • 7
  • 3

1 Answers1

0

One way to do that is to use an onEdit(e) simple trigger, like this:

/**
* Simple trigger that runs each time the user hand edits the spreadsheet.
*
* @param {Object} e The onEdit() event object.
*/
function onEdit(e) {
  if (!e) {
    throw new Error(
      'Please do not run the onEdit(e) function in the script editor window. '
      + 'It runs automatically when you hand edit the spreadsheet. '
      + 'See https://stackoverflow.com/a/63851123/13045193.'
    );
  }
  if (e.range.getSheet().getName() === 'raw Data') {
    deleteColumns();
  }
}

The function will delete columns every time you manually modify the 'raw Data' tab. To do that only when certain types of events take place, use an "on change" installable trigger.

doubleunary
  • 13,842
  • 3
  • 18
  • 51
  • onEdit won't work , since ill have to do extra inputs after this step, meaning it will keep on deleting columns, does onChange happens only once even after extra inputs? – Dice 0ne Nov 09 '22 at 11:15
  • You may be better off by running the function from call the function through some other means, such as a [button](https://developers.google.com/apps-script/guides/menus#clickable_images_and_drawings_in_google_sheets), a [custom menu item](https://developers.google.com/apps-script/guides/menus), or a [sidebar](https://developers.google.com/apps-script/guides/dialogs#custom_sidebars). Please [edit](https://stackoverflow.com/posts/74371266/edit) the question to include your exact requirements. – doubleunary Nov 09 '22 at 11:53
  • i guess a custom menu will do the trick. thanks for the help! – Dice 0ne Nov 09 '22 at 13:40