Hi all,
I have what I think is a scripting issue I’d love some eyes on… Admittedly, I’m very new to script editing, so I’d appreciate any help with this.
I’ve used the Tags column in my Transactions spreadsheet with data validation for some time now, and I’d like to add-on to it the ability to allow multiple tags (as opposed to just one).
I’ve used a very nice tutorial to add the multi-select feature (without repetition and for the tags column in my sheet). It works very well in the Transactions sheet. But, when I go to look at the Tags Report, something is awry. I can select a tag to filter on, and the transactions display for a brief moment and then they disappear, leaving behind a strange ‘undefined’ note. I’m wondering if there’s some conflicting code in the base template that may be causing the issue, or if the script I’ve added is faulty in some way.
Steps I’ve already taken to troubleshoot:
- I tried restoring previous versions (and removing the script entirely) to make sure the Tags Report worked OK before I added the script. Result: Tags Report worked fine before (without the script in place).
- Renamed the script file in Apps Script to something other than code.gs. Result: Tags Report works, but the multiselect in the Transactions sheet does not. (Yes, I did change file filename back to ‘code.gs’ and get the same result as before where the Tags Report doesn’t work, but the multiselect in Transactions does.)
- Ran the script in the Apps Script. Result: I received an error that refers to line 7 in the script (newValue=e.value;). Error reads:
TypeError: Cannot read property ‘value’ of undefined
onEdit @ code.gs:7
I’m attaching a screenshot of my current Transactions sheet, the code I’ve added to the Apps Script and a screenshot of that, plus a screenshot of the strange error I’m seeing on the Tags Report.
Thanks for taking a look!
You’ll see the multiselect data validated tags in row 10 are working! Since the multiselect feature is in place, it conflicts a little with the data validation so we see a red triangle error message saying it’s an invalid entry. This is to be expected, though, per the tutorial.
Note: at first when I select a tag in the Tags Report, everything looks normal and there’s only one tag listed in the filter list. “undefined” only appears a moment later and the transactions tagged with that tag disappear. This happens for all the tags, not just one or two specific ones.
Code:
function onEdit(e) {
var oldValue;
var newValue;
var ss=SpreadsheetApp.getActiveSpreadsheet();
var activeCell = ss.getActiveCell();
if(activeCell.getColumn() == 5 && ss.getActiveSheet().getName()=="Transactions")
newValue=e.value;
oldValue=e.oldValue;
if(!e.value) {
activeCell.setValue("");
}
else {
if (!e.oldValue) {
activeCell.setValue(newValue);
}
else {
if(oldValue.indexOf(newValue) <0) {
activeCell.setValue(oldValue+','+newValue);
}
else {
activeCell.setValue(oldValue);
}
}
}
}