Benchmark test: Write the Apps-Script function... GPT v some others

Prompted this: ‘Write the Apps-Script function to list the range names, addresses and range values in a Google Sheet. Make the list in a sheet named ‘RangeNames’ and allow for the possibility that the sheet ‘RangeNames’ may already exist.’ to ChatGPT Turbo 3.5, Meta’s LLaMA in Perplexity, Cursor IDE, GPT-4 API and Cohere’s v1/generate API in Apps-Script and Bard.

Here’s what the GPT-4 API in Apps-Script wrote:

function listRangeNames() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var rangeNamesSheet = ss.getSheetByName('RangeNames');
  
  // If the sheet 'RangeNames' doesn't exist, create it.
  if (!rangeNamesSheet) {
    rangeNamesSheet = ss.insertSheet('RangeNames');
  } else {
    rangeNamesSheet.clear(); // If the sheet already exists, clear it.
  }
  
  var rangeNames = ss.getNamedRanges();
  
  // Set the headers
  rangeNamesSheet.getRange('A1:C1').setValues([['Range Name', 'Range Address', 'Range Values']]);
  
  // Loop through each named range and add its name, address, and values to the sheet
  for (var i = 0; i < rangeNames.length; i++) {
    var rangeName = rangeNames[i].getName();
    var range = rangeNames[i].getRange();
    var rangeAddress = range.getA1Notation();
    var rangeValues = JSON.stringify(range.getValues());
    
    rangeNamesSheet.appendRow([rangeName, rangeAddress, rangeValues]);
  }
}

It worked of course The appendRow array is delicious. Perplexity provided the most interesting set of links. Bard offered to store its effort in https://colab.research.google.com/. The Cursor IDE request cost $0.56. They all worked except Bard and after 3 attempts to help it I gave up and asked ChatGPT to fix it which it duly did.