0
votes

This app script in my google sheets, can save in google drive a google doc based on dynamic value listed on various columns.

How can i save it locally instead to save it on drive?

This is the app script in the sheet:


var TEMPLATE_ID = '1mqANOhwNQ-dynlR5GO9ybC5UgGsDTz_1K8CUyXw4-SM'

var PDF_FILE_NAME = ''

function onOpen() {

  SpreadsheetApp
    .getUi()
    .createMenu('Crea Contratto')
    .addItem('Crea Contratto', 'createPdf')
    .addToUi()

}

function createPdf() {

  if (TEMPLATE_ID === '') {

    SpreadsheetApp.getUi().alert('TEMPLATE_ID needs to be defined in code.gs')
    return
  }

  var copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy(),
      copyId = copyFile.getId(),
      copyDoc = DocumentApp.openById(copyId),
      copyBody = copyDoc.getActiveSection(),
      activeSheet = SpreadsheetApp.getActiveSheet(),
      numberOfColumns = activeSheet.getLastColumn(),
      activeRowIndex = activeSheet.getActiveRange().getRowIndex(),
      activeRow = activeSheet.getRange(activeRowIndex, 1, 1, numberOfColumns).getValues(),
      headerRow = activeSheet.getRange(1, 1, 1, numberOfColumns).getValues(),
      columnIndex = 0

  for (;columnIndex < headerRow[0].length; columnIndex++) {

    copyBody.replaceText('%' + headerRow[0][columnIndex] + '%', 
                         activeRow[0][columnIndex])                         
  }

  copyDoc.saveAndClose()

  /** var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'))  

  * if (PDF_FILE_NAME !== '') {

    newFile.setName(PDF_FILE_NAME)
  } */

  // copyFile.setTrashed(true)

  SpreadsheetApp.getUi().alert('Contratto creato!')

}
1

1 Answers

1
votes

As App Script executes server-side, it is not possible to directly download a file locally to your machine.

There is a useful link here that guides through using HTML Service to run client side which can download a file locally.