0

Every time I use text in an if statement it says "Missing ; before statement." My code is

function onEdit() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Class');
var class = sheet.getRange('Traits!L3').getValue();
  if (class == 'Warlock' ) {
    sheet.getRange('A7:D7')breakApart();
    sheet.getRange('A7:D7')mergeAcross();
  } else {
    if (class == 'Fighter') {
      sheet.getRange('A7:D7')breakApart();
      sheet.getRange('A7:B7')mergeAcross();
    } else {
    }

What it does is break and merge cells based on your class (Warlock and Fighter). Traits!L3 is a data validation that lets you choose a class so if you choose warlock, then it merges 4 cells, if not, (unfinished code). How and why does this show up and is there a way to fix it?

2
  • change your variable name from class to something else. Commented Dec 19, 2019 at 4:27
  • It looks like your problem can be completely solved without Google App Scripts, why can't breakApart mergeAcross just be done with regular formulas do they really need to be scripts? Commented Dec 19, 2019 at 4:33

1 Answer 1

3

You forgot a . before breakApart() and mergeAcross()

function onEdit() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Class');
var char_class = sheet.getRange('Traits!L3').getValue();
  if (char_class == 'Warlock' ) {
    sheet.getRange('A7:D7').breakApart();
    sheet.getRange('A7:D7').mergeAcross();
  } else {
    if (char_class == 'Fighter') {
      sheet.getRange('A7:D7').breakApart();
      sheet.getRange('A7:B7').mergeAcross();
    } else {
    }

You should use Data->Named Ranges in google sheets and start naming your cell references then you can call them in code or even formulas by that name.

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

7 Comments

@HunterLindsey you can always mark as the answer and vote up now or later. Also I recommend not using Google App Scripts and to use built in formulas as much as you can because GAS is slow and unreliable.
@HunterLindsey Your question is why were you getting a semicolon error and this resolves that issue, as to whether your code works is an entirely different question as I suggested I would ask a new question on how to solve some of your specific problems without GAS because it often is not necessary. Basically from what I see with your code I don't agree with doing it this way (I would just do regular formulas) but that doesn't mean you can't get it working. You should post a link to your sheet otherwise I have no idea what you are even working with.
I don't find Google Apps Script slow or unreliable. Personally, I avoid cell functions
@Cooper onEdit can only run simultaneously twice and then it ignores all other edits, so if you edit too quickly or have too many people editing at once you will not be guaranteed an onEdit execution, also every time you access the spreadsheet to read or write Google specifically says it is slow and you should do it in batches. A cell function is guaranteed to work.
Yes. I never use onEdit in any of my projects. It's a recipe for disaster which most new users use it way too much. And I never let multiple user edit directly from spreadsheet, I always force them to work through a webapp or I won't do the project.
|

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.