qqq
qqq

Reputation: 1

I want it to be created with a different name after copying the file to the folder I need

I take files from a spreadsheet in Excel and replace the substituted words in word, copying the file to a separate folder with the finished documents.

I work in Excel. I want it to be renamed to the name I need when copying the file.

Tell me how this can be done. I tried to find different methods, but not one came up

function myFunction() {
  const docFile = DriveApp.getFileById("1OLAGN_5NN1ZEudjlAjfnESu3auGHUxVHlwAktbm24OQ");
  const tempFolder = DriveApp.getFolderById("10AU7yahd5PogWHK0m_mOUZvFS-pbu2g8");
  const tempFile = docFile.makeCopy(tempFolder);
  const tempDocFile = DocumentApp.openById(tempFile.getId());
  const body = tempDocFile.getBody();

  var list = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  var a = list.getRange(2, 3).getValue()
  var b = list.getRange(3, 3).getValue();
  var c = list.getRange(4, 3).getValue();

  body.replaceText("{Имя}", a);
  body.replaceText("{Дата}", b);
  body.replaceText("{Сумма}", c);

  tempDocFile.saveAndClose();
}

Upvotes: 0

Views: 57

Answers (1)

BLAKE
BLAKE

Reputation: 313

You can use the setName(newName) method on the file object. This allows you to set a new name for the copied file after you've made the necessary text replacements.

Source: https://developers.google.com/apps-script/reference/drive/file#setnamename

function myFunction() {
  const docFile = DriveApp.getFileById("1OLAGN_5NN1ZEudjlAjfnESu3auGHUxVHlwAktbm24OQ");
  const tempFolder = DriveApp.getFolderById("10AU7yahd5PogWHK0m_mOUZvFS-pbu2g8");
  const tempFile = docFile.makeCopy(tempFolder);
  const tempDocFile = DocumentApp.openById(tempFile.getId());
  const body = tempDocFile.getBody();

  var list = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  var a = list.getRange(2, 3).getValue(); 
  var b = list.getRange(3, 3).getValue();
  var c = list.getRange(4, 3).getValue();

  body.replaceText("{Имя}", a);
  body.replaceText("{Дата}", b);
  body.replaceText("{Сумма}", c);

  tempDocFile.saveAndClose();

  // After making replacements and saving the document, rename the file.
  // You can use a string you prefer for the new file name.
  const newName = "New Name";
  tempFile.setName(newName);
}

Upvotes: 0

Related Questions