I would think there would be several balances and practical considerations.
Germany would have to balance in 1940 whether a neutral Spain is likely to remain neutral and what the benefits might be (and let's face it only two European nations managed to remain neutral during the war, as much because it was difficult or unproductive to change the status quo.
In the heady days of June-September 1940 its probably not thought to be worth it and Germany would want to subdue Britain before sending its armies further south. Also with Vichy France formed, the need for invasion is less as North West Africa is secure and the existence of Vichy might persuade the Spaniards to remain neutral to avoid invasion.
Dec 1940-1941 is trickier, with the war in the Med going badly for the Italians the Germans would probably still get involved in Greece and Yugoslavia. That would tie up troops for any Spanish invasion. However, by mid-1941 the Germans and Italians would be eager to capture Gibraltar. That would mean either diplomatic pressure to get access or invasion. With troops in Greece that would mean probably a campaign could not start until June/July. By then Barbarossa is in full swing, so its one or the other (Barbarossa would always be Hitler's number one priority) or the Afrika Korps is not sent to North Africa.
The Brits being hard to meet Churchill's demands to send troops to Greece would be unable to send troops to protect Spain. If Spain was attacked in 1941 there would be little the British Army could do to support it and if reinforcements for the 8th Army or Far East were sent there and lost, it could easily endanger the loss of Egypt or quicker defeat in the Far East in early 1942.
1942. If Spain somehow remains neutral, with Malta being a pain it might be tempting to revive any invasion plans to get at Gib. Britain still pinned down in Egypt can't seriously reinforce Spain either and with Russia getting war goods it means decisions either way. Could Germany spare the troops to launch a major offensive given the Eastern Front demands? Germany might be getting materials through the blockade, but that's going to invite Allied criticisms. Also, would the Allies be pushing for bases in the Canaries to combat the U-boots? Germany might want access to Gib and Canary bases too. Occupying Vichy might drive the impetus to seal the potential breach further by occupying Spain, but could it be done?
The Americans might see a Torch in Spain as a way to open the Second Front. Churchill might buy this, but would Spain buy into such an idea and risk seeing their nation destroyed (especially as the allies since 1940 have been consistently losing)?
1943. Spain would be safe, if not attacked now by Germany, it never will be. Again, Churchill's soft underbelly might include Spain, now the war is turning Spain might come in on the Allied side with full US/UK resources behind it (M4s, B-25s, P-40s etc.).
So I think the question boils down to whether Germany wants to risk its blockade busting access, if they want to then June 1941-Nov 1942 is the key time to do so.