谷歌的重复脚本

时间:2015-01-22 21:02:41

标签: google-apps-script google-sheets

完成!!! 非常感谢Ilya Lapitan和我的朋友Zent。你救我 :) 要做什么脚本:

  • 重复模板表;
  • 将新工作表重命名为:“数字”+“基本工作表中的名称”(其“All_base”,其中包含名称列表)

    function makeCopies() {
        ///getting active spreadsheet
        var activeSreadsheet = SpreadsheetApp.getActiveSpreadsheet();
        var basicSheet = activeSreadsheet.getSheetByName("All_base");
        var activeSreadsheet2 = SpreadsheetApp.getActiveSpreadsheet();
        var templateSheet = activeSreadsheet2.getSheetByName("1. Win");
    
        //setting template sheet as active
        activeSreadsheet2.setActiveSheet(templateSheet);
    
        //getting values from selected range
        var rangeArr = basicSheet.getRange("B4:B6").getValues();
    
        //going through range values
        for(i = 0; i < rangeArr.length; i++){
    
        //preparing name for new sheet
        var name = (i + 2) + '. ' + rangeArr[i][0];
    
        //making duplicate of base sheet
        var newSheet = SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
    
        //setting new name for duplicated sheet
        newSheet.setName(name)
        }
    };
    

1 个答案:

答案 0 :(得分:1)

这是一个按选定范围制作基础表副本的解决方案:

function onOpen() {
   var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
   var entries = [{
     name : "Make Copies",
     functionName : "makeCopies"
   }];
   spreadsheet.addMenu("Duplicate", entries);
};

function makeCopies() {
   //getting active spreadsheet
   var activeSreadsheet = SpreadsheetApp.getActiveSpreadsheet();
   //getting basic sheet
   var basicSheet = activeSreadsheet.getSheetByName("All_base");
   //setting basic sheet as active
   activeSreadsheet.setActiveSheet(basicSheet);
   //getting values from selected range
   var range = basicSheet.getRange("D7").getValue().split(", ");
   //going through range values
   for(i = 0; i < range.length; i++){
      //preparing name for new sheet
      var name = (i + 1) + '.' + range[i];
      //making duplicate of base sheet
      var newSheet = SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
      //setting new name for duplicated sheet
      newSheet.setName(name)
   }
 };

使用示例:

  1. 从基本表格中选择范围
  2. 选择一个菜单项:&#34;复制&#34; - &GT; &#34;制作副本&#34;