如何基于命名范围自动创建工作表

时间:2019-03-28 16:05:36

标签: google-apps-script google-sheets

我希望能够使脚本基于基础工作表中的命名范围来创建新的模板工作表。还要检查是否有重复的名称,如果存在重复的名称,则不要创建新的工作表。

function test() {
 var ss        = SpreadsheetApp.getActiveSpreadsheet();

   // Get the range of cells that store employee data.
  var employeeDataRange = ss.getRangeByName("EmployeeRef");

  var employeeObjects = employeeDataRange.getValues();

  var template = ss.getSheetByName('Template');

  for (var i=0; i < employeeObjects.length; i++) {

     // Put the sheet you want to create in a variable
     var sheet = ss.getSheetByName(employeeObjects[i]);

      // Check if the sheet you want to create already exists. If so,
      // log this and loop back. If not, create the new sheet.
        if (sheet) {
           Logger.log("Sheet " + employeeObjects[i] + "already exists");
        } else {
           template.copyTo(ss).setName(employeeObjects[i]);
           }
        }
  return;

0 个答案:

没有答案