Вот мой подход:
function myFunction() {
var names = ['John', 'Richard', 'Bill', 'Bob', 'Walter'];
var result = []
var sheet = SpreadsheetApp.getActive().getSheetByName("sheet1")
names.forEach( (element, index) => {
names.slice(index+1,names.length).forEach ( (rest) => {
result.push([element,rest])
})})
sheet.getRange(2,1,result.length,result[0].length).setValues(result);
}
Explanation:
Essentially you iterate through the names list. Starting from each element you only concatenate with the next elements. For example, John is the first element in the name list, you concatenate John with Richard, Bill, Bob and Walter. Then you go to the next element which is Richard and you concatenate him with Bill, Bob and Walter.
In this way, you avoid having duplicates like: John - John or John - Richard and Richard - John. The forEach method helps you achieve this iterative approach.
References:
forEach ()