Would it actually be legal to alternate between Andorra and France or Spain?
Yes, on condition that when entering Andorra you have a Schengen Area exit stamp and when leaving Andorra have a Schengen Area entry stamp.
As far as the Schengen Border Code, is concerned, it is the proper entry/exit stamps that counts (assuming you don't require a visa) for short term stays.
Without the entry/exit stamps you are assumed to have overstayed (Article 12 (1,4)), unless you can prove otherwise (Article 12 (2)).
Schengen Border Code
Article 12
Presumption as regards fulfilment of conditions of duration of stay
(1) If the travel document of a third-country national does not bear an entry stamp, the competent national authorities may presume that the holder does not fulfil, or no longer fulfils, the conditions of duration of stay applicable within the Member State concerned.
(2) The presumption referred to in paragraph 1 may be rebutted where the third-country national provides, by any means, credible evidence, such as transport tickets or proof of his or her presence outside the territory of the Member States, that he or she has respected the conditions relating to the duration of a short stay.
...
(4) The relevant provisions of paragraphs 1 and 2 shall apply mutatis mutandis in the absence of an exit stamp.
Sources:
- How to Get a Residence Permit in Andorra | Andorra Guides
Section 4
...
The regime for entry and movement inLe régime d'entrée et de circulation en France and Spain for foreigners holding an Andorran residence permit is identical to that which applies to foreigners holding a French or Spanish residence permitet en Espagne des étrangers titulaires d'un titre de séjour andorran est identique à celui qui s'applique aux étrangers titulaires d'un titre de séjour français ou espagnol.Google Translate Original textThe regime for entry and movement in France and Spain for foreigners holding an Andorran residence permit is identical to that which applies to foreigners holding a French or Spanish residence permit.
Google Translate Original text
- Andorra Schengen Visa Explained | Andorra Guides