The answer is “yes”, but before going for a relatively complex task, think about your problem.
If this is only a matter of “contextual” spacing, you can solve it elegantly by configuring your paragraph styles.
You have two spacing parameters: Above and Below. Usually people customise only Above, leaving Below set to 0.
Your context seems to be Heading 1 vs. Text Body may precede Heading 2.
A wider space between Heading 1 and Heading 2 than between Text Body and Heading 2 is solved by setting a Below spacing in Heading 1.
The reverse case cannot be solved in this manner because it is not possible to give negative values to spacings. You must then create a custom paragraph style, say H2. Whether you define it as based on Heading 2 (with right-click on Heading 2 in the style sidepane and
New) or independent (i.e. based on Default Style) is a matter of personal choice.
Outline & Numbering tab, select from Outline level drop-down menu the level to assign to the style, here
Level 2. Things become complicated if your chapters are numbered because you cannot attach a user style to
Chapter Numbering Numbering style. You must create your own counter with a dedicated list style (accessed through the fifth icon from the left in the toolbar). But, CAUTION, this counter is not related at all with the chapter number. This means you have two independent chapter numbering which is not what you expect. To get a unified numbering, you must redefine the Heading n family to use your counter (which is very tricky) or create a new Hx family with variants in levels and associate all styles to your counter.
One last word: the custom styles are attached to an outline level but the user-chapter counter is not synchronised with the outline level. To force the counter to the desired level, you must press
Tab (at start of heading) to promote the paragraph to the required level: none for level 1, one for level 2, n-1 for level n.
To show the community your question has been answered, click the ✓ next to the correct answer, and “upvote” by clicking on the ^ arrow of any helpful answers. These are the mechanisms for communicating the quality of the Q&A on this site. Thanks!
In case you need clarification, edit your question (not an answer which is reserved for solutions) or comment the relevant answer.