Melt 6 tablespoons of butter in a large skillet and cook 2 large thinly sliced onions for 20-30 minutes, stirring often, until deeply caramelized. Add ½ teaspoon of salt and ½ teaspoon of sugar partway through.
Meanwhile, beat 2 cups of all-purpose flour, 1½ teaspoons of salt, 4 large eggs, and ½ cup of milk with the paddle of a stand mixer for about 10 minutes, until bubbles appear.
Preheat the oven to 400°F / 205°C. Bring a large pot of lightly salted water to a boil, then lower to a simmer.
Press the batter through a spätzle maker into the water in batches. Cook for 2-3 minutes, until the spätzle float, then lift into ice water for 1-2 minutes and drain well.
Layer the spätzle, onions, and 340g of shredded Emmentaler in a buttered 9 × 13in / 23 × 33cm baking dish. Bake uncovered for about 10 minutes, until the cheese melts and the edges start to crisp.