'Google Apps Script URL being converted to text with copy row

The following code copies a row from one sheet and adds it to the next free row of another sheet. However, one of the cells in the source range contains a hyperlink to a Google Drive folder. When the range is added to the bottom of the target sheet the hyperlink is stripped out leaving just the folder name in text. How can I retain the hyperlink during the copy process?

My Code:

function updateProjectLog() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName('Metadata');

  var sourceRange = sourceSheet.getRange('metadataRecord');
  var sourceValues = sourceRange.getValues();

  var rowCount = sourceValues.length;
  var columnCount = sourceValues[0].length;

  var targetSheet = ss.getSheetByName('Project Log');
  var lastRow = targetSheet.getLastRow();
  var targetRange = targetSheet.getRange(lastRow + 1, 1, rowCount, columnCount);

  targetRange.setValues(sourceValues);

}


Solution 1:[1]

Use RichTextValues to manage stylized text, including links:

function updateProjectLog() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName('Metadata');
  var sourceRange = sourceSheet.getRange('metadataRecord');
  var sourceValues = sourceRange.getRichTextValues();
  var rowCount = sourceValues.length;
  var columnCount = sourceValues[0].length;
  var targetSheet = ss.getSheetByName('Project Log');
  var lastRow = targetSheet.getLastRow();
  var targetRange = targetSheet.getRange(lastRow + 1, 1, rowCount, columnCount);
  targetRange.setRichTextValues(sourceValues);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Iamblichus