Neither Juan Carlos nor de Gaulle were in power at the relevant point. They both held huge moral authority though. One used it to defend democracy and the rule of law; the other used it to put himself into power.
De Gaulle was unable to ´defend democracy´ as Juan Carlos was: Spain in 1981 was a working democracy, France at the end of the 1950s was faced with an impossible problem: how to end the bloody conflict in Algeria and prevent a military coup. De Gaulle did both and more: no military coup occurred, democracy was restored in a working form, and Algeria gained independence. It´s hard to see how the forces at work in the Algerian question could have solved all of this on their own, while keeping democracy as was.
(I remember seeing the images of Spanish parliament in 1980 on tv; and yes, it was shocking - but mostly it was: What is this idot colonel thinking? Parliament, although most of its members were cowering under the tables, was obviously not going to grant his wish, so all he could do was appeal to the king - who, as you say, wasn´t in power -, totally misjudging the king´s response, which effectively curbed a coup that had few support in the military to begin with.)
So the end justifies the means? If you're disappointed with a liberal democratic government, then you stand for office, you don't sulk in the hope that someone will organize a coup for you. It's also surprising to see Algeria cited as an example of his greatness. He was put into power by those who wanted to keep Algeria in France forever. He promptly executed the opposite policy.
The latter is precisely my point. Algeria was the key to the problem the republic faced, and De Gaulle solved what no ´liberal democratic government´ could. Had he declined the offer to take power (the coup was not his, as mentioned), nothing constructive would have happened. As to the end justifying the means, if the means was De Gaulle, and the end solving a problem the then republic could not resolve, then yes. As a general rule: no. De Gaulle may have had his qualms about democracy, but he never abandoned it; this is exactly the trap the military that called upon him fell into.