- You want to convert the values of above image to the values of below image.
If my understanding is correct and you use Google Apps Script, how about this sample script? I think that there are several solutions for your situation. So please think of this as just one of them.
- In this sample script, it supposes that the initial values are put to the cells of "A1:D4" in the active sheet. And the result is put to the cells of "A6:C23".
- The point of this sample script is that all patterns are retrieved by transposing the values.
Sample script:
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getRange("A1:D4").getValues(); // Retrieve the initial values
var ar = [];
for (var loop = 1; loop >= 0; loop--) {
var temp = [];
for (var i = 1; i < values.length; i++) {
for (var j = 1; j < values[i].length; j++) {
temp.push([values[0][j], values[i][0], values[i][j]]);
}
}
ar[loop] = temp;
values = values[0].map(function(_, i) {return values.map(function(f, j) {return f[i]})}); // Transpose the initial values.
}
var res = Array.prototype.concat.apply([], ar);
sheet.getRange("A6:C23").setValues(res); // Put the result
Result:

- "A1:D4" is the initial values.
- "A6:C23" is the result values.
Note:
- This is a simple sample script. So please modify it for your situation.
If I misunderstood your question, I apologize.