Those trying to lose weight can bake low calorie (and whatever other qualifiers fit their diet - low carb, high fiber, whatever) stuff and bring it over for everyone including themselves to enjoy. That way they don't have to skip treats every time.
Earlier this year I decided to make some almond flour muffins and brought them to the office. Everyone loved them.
Parties are a slightly different thing. It all comes down to timing and availability. If you're being very strict with your diet at the time, then you'll just have to suck it up. If not, then a piece of cake will not make any difference. If there are other options available, go for them. There are usually some peanuts lying around at parties and since I rarely eat peanuts I saw them as a treat.
There was this one time when there was an office party on the same day as I was IF'ing (I would have switched the days, but I didn't know about it until it started, due to all the emails being sent to everyone's university email which sucks and which I ignore). Just had to suck it up.
I guess it would be easier with multiple people watching their diets at the same time. Other than the obvious aspect of support, they can make sure they provide alternatives at parties and as regular treats.
On the other hand I did have ice cream every single week when leaning out last year, so what the hell do I know.