1

The big picture problem is I am adding a date to cell one week from today when an adjacent cell is edited. So far so good. However, it needs to not change when the irl date changes, or if the adjacent cell is edited a second time.

Basically been trying to check and see if there is already a date in the cell, and if so, simply returning. But it isn't doing the string comparison properly for some reason. It never recognizes it as an empty string. Then I try to use includes(), it tells me that includes() cannot be used on an object. My understanding is getDisplayValue() should always return a string? A bit at a loss.

function getDate(progress) {
 //progress: cell showing progress state (Not started, begun, completed)

  var range = SpreadsheetApp.getActiveRange();
  var d = range.getDisplayValue();

  if(progress == 'Completed') { return 'done'; } 
  if(d != '') { return; } //if due date cell already has a date, don't change it
  today = new Date();
  due = new Date();
  due.setDate(today.getDate() + 7); //one week from today
  return due;
}

Any idea what the issue is with this code? Or open to another solution if there is something more elegant.

1
  • Try Logger.log({d:d,type:typeOf d}) Commented Oct 30, 2019 at 3:57

1 Answer 1

1
  • You want to run the script when the value of the active range is the date object.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

Modification point:

  • In order to confirm whether the value of the active range is the date object, I used getValue() for retrieving the value and Object.prototype.toString.call(d).slice(8, -1) == 'Date' for the if statement.

Modified script:

function getDate(progress) {
  Utilities.sleep(5000); // <--- Added

 //progress: cell showing progress state (Not started, begun, completed)

  var range = SpreadsheetApp.getActiveRange();
  var d = range.getValue(); // Modified

  if(progress == 'Completed') { return 'done'; } 
  if (d && Object.prototype.toString.call(d).slice(8, -1) == 'Date') { return; } // Modified
  today = new Date();
  due = new Date();
  due.setDate(today.getDate() + 7); //one week from today
  return due;
}
  • When the function of getDate is run, if the value retrieved by getValue() is the date object, Object.prototype.toString.call(d).slice(8, -1) == 'Date' returns true. So the script below the if statement is not run.

References:

If I misunderstood your question and this was not the result you want, I apologize.

Sign up to request clarification or add additional context in comments.

9 Comments

This seems really close, but what's happening now is that if I edit a cell, prompting a "Loading..." message, I believe it's actually passing in that string instead of the original content of the cell.
@JRHB Thank you for replying. I apologize for the inconvenience. would like to confirm whether my understanding for your situation is correct. In your situation, getDate is run by the OnEdit event trigger. When the active range is edited, Loading... is displayed in the cell. Under this situation, the function of getDate is run. By this, d of var d = range.getValue(); returns Loading.... Is my understanding correct?
Yep, that's the issue.
@JRHB Thank you for replying. Unfortunately, I couldn't replicate your situation. I could confirm that even when the active cell shows Loading... for 10 seconds, var d = range.getValue(); of the script which was run by the OnEdit event trigger returns the calculated value which is not Loading.... So although I'm not sure whether this is the direct solution, I updated my answer. Could you please confirm it? Utilities.sleep(5000); is put to the top of function. I'm not sure whether 5000 is suitable. So please modify this. If that was not the result you want, I apologize.
@TheMaster Thank you for your comment. Yes. instanceOf can be also used. In this case, instanceOf can be simpler modification.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.