I don't understand why the gravitational potential energy wasn't taken into account.
The reasoning goes as follows. Suppose we have $U_g(x)= m g x$ and $U_e(x)= \frac{1}{2} k x^2$ where $x=0$ is at the unstretched position. Then the minimum total potential is found at $\frac{d}{dx}(U_g(x)+U_e(x))=0$. Call that minimum point $x_0=-gm/k$.
Now, we can make a coordinate transform $x \rightarrow X - x_0$ and we can write the total potential as $$U_g(X) + U_e(X) = \frac{k}{2} X^2-\frac{g^2 m^2}{2 k}$$ but since the constant term drops out of all of the equations of motion we can drop it from the Lagrangian without changing anything so we can get a simplified potential $$U_s(x)=\frac{k}{2} x^2 = U_e(x)$$$$U_s(X)=\frac{k}{2} X^2 = U_e(X)$$
So the reason that we neglect the gravitational potential energy is that if we set our coordinate $x$$X$ to be zero at the minimum potential (the equilibrium point) then the only effect of the gravitational potential is a constant offset, which can be dropped.