简体   繁体   中英

Duplicating and renaming Google Sheet automatically

I am creating employee rosters for each division at my job that need to be checked daily. I have the roster and I would like to create a certain date range worth in a workbook for each division. I have the first sheet dated 090120, for example, and need to duplicate and rename up to 091220. Is there an easy way to do this without having to right click, duplicate, right click, rename on each tab? All the information for each one is identical and there are no formulas or anything like that. I am new to Google Sheets and am not tech savvy at all but there has to be an easier way to do this.

Solution:

function duplicateSheet(){ 

const ss = SpreadsheetApp.getActive();
const src_sheet = ss.getSheetByName('090120');
const dupl_sheets = ['090220','090320','090420','090520','090620',
                     '090720','090820','090920','091020','091120','091220'];

  dupl_sheets.forEach(sheet=>{            
  var dupl_sheet=src_sheet.copyTo(ss);
  dupl_sheet.setName(sheet);                   
                      });
}

Explanation:

  • dupl_sheets contains the names of the duplicate sheets you want to create.
  • forEach() is used to iterate over dupl_sheets and for every sheet name it creates a sheet that is a duplicate of src_sheet .

Instructions:

  1. Click on Tools => Script editor :

第1步

  1. Copy/Paste the aforementioned code into the script editor and click run :

第2步


Result:

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM