Reputation: 1127
Hi everyone,
I have a google sheet with 2 tabs inside. The data are all come from another google sheet by using Importrange
formula. I want to duplicate this google sheet by using google apps script while preserving the conditional formatting but at the same time remove the Importrange
formula. In other words, there should be no Importrange
formula in the duplicated sheet but still have all the formatting and data. Is there any way I can achieve my goal? There are some similar questions on internet but did not perform exactly what I want. Any help will be greatly appreciated!
Upvotes: 1
Views: 171
Reputation: 27380
The logic is the following:
function duplicateSpreadsheet() {
const ss = SpreadsheetApp.getActive();
const ssid = ss.getId();
const folderId = DriveApp.getFileById(ssid).getParents().next().getId();
const title = ss.getSheetByName("Data1").getRange("E2").getDisplayValue(); //added
const new_ss = SpreadsheetApp.create(title);
const dest_folder = DriveApp.getFolderById(folderId);
DriveApp.getFileById(new_ss.getId()).moveTo(dest_folder);
const sheets = ss.getSheets();
sheets.forEach(sh=>{
let shDup = sh.copyTo(ss);
shDup.getDataRange().setValues(shDup.getDataRange().getValues());
shDup.copyTo(new_ss).setName(sh.getName());
ss.deleteSheet(shDup);
});
new_ss.deleteSheet(new_ss.getSheetByName("Sheet1"));
}
Upvotes: 2