需要帮助在Google Apps脚本中的同一个电子表格中复制和编辑工作表

时间:2014-03-24 20:25:49

标签: google-apps-script

我需要一些帮助来改进这段代码。在这种情况下,它应该根据工作表Maxes上的行复制特定工作表。然后将工作表重命名为工作表Maxes中给定行上的名称。当我尝试执行代码时,它只是插入一个空白页面并将其添加到电子表格的末尾。请原谅我编写的代码写得不好的代码,并努力使某些工作成功。我们将非常感谢您提出的任何建议。我不确定while循环中是否有错误或究竟是什么。

function createWorkouts(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var nameSource = ss.getSheetByName("Maxes");
var nameColumn = nameSource.getRange("A2:A300");
var nameStartRow = nameSource.getRange("A2");

var trainingSheet = ss.getSheetByName("Master Copy");
var trainingRange = trainingSheet.getRange("a1:m66");

var nameEndRow = nameSource.getLastRow();
var lifterName;


while (nameStartRow <= nameEndRow)
{
 lifterName = nameSource.getCell(nameStartRow, nameColumn);

 if (lifterName == null)
 {
  errors.clear();
  ss.getSheetsByName(lifterName) = lifterName;
  if(errors.number() > 0)
  {
    errors.clear();
    var newSheet = ss.insertSheet();
    ss.renameActiveSheet(lifterName);
    ss.getSheetByName(lifterName);
    trainingRange.copyValuesToRange(lifterName,1,1,16,66);
    lifterName.copyValuesToRange(lifterName,4,1,4,1);
   }
  } nameStartRow = nameStartRow + 1;
 }
}

1 个答案:

答案 0 :(得分:0)

我修改了你的代码,因为我没有完全理解你想要的东西,我希望这会有所作为:

function createWorkouts(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var nameSource = ss.getSheetByName("Maxes");
  var nameColumn = nameSource.getRange("A2:A").getValues(); // it's a table of table [][]

  var trainingSheet = ss.getSheetByName("Master Copy");
  var trainingRange = trainingSheet.getRange("A1:M66").getValues(); // get values give a table of table

  for(var i in nameColumn){ 
    if(nameColumn[i][0]!=""){ // if blank we skip
      try{ // try to see if already exist who know...
        var lifter = ss.getSheetByName(nameColumn[i][0]).activate();
        Logger.log("getting lifter sheet "+nameColumn[i][0]);
      }
      catch(err){ // well it didn't exist so lets create it :D
        var lifter = ss.insertSheet(nameColumn[i][0]);
        Logger.log("creating lifter sheet "+nameColumn[i][0]);
      }
      SpreadsheetApp.flush();
      lifter.getRange("A1:M66").setValues(trainingRange); // passing the value to the created sheet
    }
  }
}

我还有一个演示表here 请告诉我这是否是您想要的(如果不是,您可以提供您想要的演示表以及最后应该获得的演示)

修改

这里是带有公式副本的脚本(它不好,因为你被迫在脚本中定义你的公式,我们可以让脚本分析电子表格来搜索公式,但是,有它&# 39;来自模板的副本,它没有那么少的资源消耗。)

function createWorkouts(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var nameSource = ss.getSheetByName("Maxes");
  var nameColumn = nameSource.getRange("A2:A").getValues(); // it's a table of table [][]

  var trainingSheet = ss.getSheetByName("Master Copy");
  var trainingRange = trainingSheet.getRange("A1:H21").getValues(); // get values give a table of table
  var trainingFormulasD = trainingSheet.getRange("D2:D21").getFormulas(); // get the formulas to copy
  var trainingFormulasH = trainingSheet.getRange("H2:H21").getFormulas(); // get others formulas to copy

  for(var i in nameColumn){ 
    if(nameColumn[i][0]!=""){ // if blank we skip
      try{ // try to see if already exist who know...
        var lifter = ss.getSheetByName(nameColumn[i][0]).activate();
        Logger.log("getting lifter sheet "+nameColumn[i][0]);
      }
      catch(err){ // well it didn't exist so lets create it :D
        var lifter = ss.insertSheet(nameColumn[i][0]);
        Logger.log("creating lifter sheet "+nameColumn[i][0]);
      }
      SpreadsheetApp.flush();
      lifter.getRange("A1:H21").setValues(trainingRange); // passing the value to the created sheet
      lifter.getRange("D2:D21").setFormulas(trainingFormulasD); //passing formulas
      lifter.getRange("H2:H21").setFormulas(trainingFormulasH); //passing formulas
    }
  }
}