Horaciux
Horaciux

Reputation: 6477

how to create a duplicate a copy from a template google spreadsheet

I have two google spreadsheets. One is a template, and the other is a Master spreadsheet containing people's names, emails, and a link to a copy of the template spreadsheet.

I wanted to automate part of the process, but I'm new to GAS and JavaScript.

I need help copying the template and access the link to this new file, so I can add this link to the master spreadsheet.

Upvotes: 0

Views: 967

Answers (1)

NightEye
NightEye

Reputation: 11204

I have provided a sample code below showing what it will look like. You can start from here:

function copyTemplateAddLink() {
  // Sheet IDs are found in the url after "/d/", see images below
  var masterID = '1CGsdAm2ewyUYyOPcTF97uxkbdAhNlxstCH09f9BHnZs';
  var templateID = '1hJ0Ze04NeCUzG8tkWPfUZOuHhEQb-dvocquRUuwnqrM';

  // Setting link column to 3 as sample
  var linkColumn = '3';
  
  var masterSheet = SpreadsheetApp.openById(masterID);
  var templateSheet = SpreadsheetApp.openById(templateID);

  // Copy template
  var templateCopy = DriveApp.getFileById(templateSheet.getId()).makeCopy("Copy of Template Sheet");

  // Get url/link of the said copied template and then add it next to the bottom row, on column set above
  masterSheet.getActiveSheet().getRange(masterSheet.getLastRow() + 1, linkColumn, 1, 1).setValue(templateCopy.getUrl()); 
}

Master before running the code:

master

Template:

template

Copy of Template generated:

copy

Master after running the code:

master after

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 1

Related Questions