Search code examples
google-apps-scriptgoogle-sheetsgoogle-docs

Is there a way to get the format of a cell in Google Sheet and move it to Google Doc for reporting purpose?


Right now, i'm using a script for Google sheet that takes the value of a cell, creates a Google Doc and then replace key phases with the value of the cell I have chosen. It works, but the formatting does not carryover. I need the values to be in percent format and not in decimal form. Any idea on how to make that happen? The script is below:

  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sprint Reference");

  var percentworkaccpected = sheet.getRange("H5").getValue();

var templateFile = DriveApp.getFileById(“File ID");
  var templateResponseFolder = DriveApp.getFolderById(“Folder ID”);

var copy = templateFile.makeCopy('Purple Team ' + timestamp + ' Sprint', templateResponseFolder)

  var doc = DocumentApp.openById(copy.getId());

  var body = doc.getBody();
body.replaceText("{{Percent Work}}", percentworkaccpected);

 doc.saveAndClose();

}```



Solution

  • Use getDisplayValue() to retain the formatting.

    Replace

    var percentworkaccpected = sheet.getRange("H5").getValue();
    

    with

    var percentworkaccpected = sheet.getRange("H5").getDisplayValue();
    

    References: