0

Good morning,

I'm very new to scripting and I have two scripts on my file that work fine independently (either one or the other) but I will need both scripts to run at the same time at all times . Here are my two scripts hoping you can help me get them working simultaneously.

Script 1:

/**
* Creates a Date Stamp if a column is edited.
*/

//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK = 2;
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,4];
// Sheet you are working on
var SHEETNAME = 'Work'


function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//checks that we're on the correct sheet.
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
//checks the column to ensure it is on the one we want to cause the date to appear.
if( selectedCell.getColumn() == COLUMNTOCHECK) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}

Script 2

/**
* Creates a Date Stamp if a column is edited.
*/

//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK = 4;
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,4];
// Sheet you are working on
var SHEETNAME = 'Work'


function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//checks that we're on the correct sheet.
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
//checks the column to ensure it is on the one we want to cause the date to appear.
if( selectedCell.getColumn() == COLUMNTOCHECK) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}

As you can see the two are similar but one is to enter the date of the day when a column is modified and the other is to enter the date of the day when another column is modified. I entrust you my file to be more clear.

https://docs.google.com/spreadsheets/d/1n3yOUo86zoArPVeecerl9Pfblq53gxTinfPAe6xIgRk/edit?usp=share_l...

1

1 Answer 1

0

Since the scripts are the same with the exception of the var COLUMNTOCHECK you just need to run one of the scripts but have it check if the column matches one of two values.

Change 1

Original:     var COLUMNTOCHECK =

Replacement:  var COLUMNTOCHECK01 =
              var COLUMNTOCHECK02 =

Change 2

Original:     if( selectedCell.getColumn() == COLUMNTOCHECK) {

Replacement:  if( ( selectedCell.getColumn() == COLUMNTOCHECK01 ) ||
                  ( selectedCell.getColumn() == COLUMNTOCHECK02 ) ) {

New Code

/**
* Creates a Date Stamp if a column is edited.
*/

// CORE VARIABLES
// The columns you want to check if something is entered.
var COLUMNTOCHECK01 = 2;
var COLUMNTOCHECK02 = 4;
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,4];
// Sheet you are working on
var SHEETNAME = 'Work'

function onEdit(e) {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  //checks that we're on the correct sheet.
  if( sheet.getSheetName() == SHEETNAME ) {

    var selectedCell = ss.getActiveCell();
   
    //checks the column to ensure it is on the one we want to cause the date to appear.
    if( ( selectedCell.getColumn() == COLUMNTOCHECK01 ) ||
        ( selectedCell.getColumn() == COLUMNTOCHECK02 ) ) {
    
       var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
       dateTimeCell.setValue(new Date());
    }

  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.