将单个工作表复制到Google云端硬盘中的多个工作簿

时间:2015-05-19 18:06:08

标签: google-apps-script google-sheets

我正在尝试将单个工作表复制到Google云端硬盘中的多个工作簿。就在一个多月前,这段代码运行良好,但现在却没有,因为我认为代码中的DocsList命令已被谷歌弃用(参见https://developers.google.com/apps-script/sunset)。我听说新命令是DriveApp,但我不太确定。在编写代码时,我是一个新手,所以我希望有人可以修改这段代码吗?

function onOpen(){
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [];
  menuEntries.push({name: "Copy Active Sheet to Other Spreadsheets", functionName: "doGet"});
  spreadsheet.addMenu("Copy Sheet", menuEntries);
}

function doGet()
{
  var app = UiApp.createApplication();
  app.setTitle("Kishan - Copy Sheet in Multiple Spreadsheets");

  var form = app.createFormPanel();
  var flow = app.createFlowPanel();

  var label = app.createLabel("Select Spreadsheet where you want to copy the current sheet:").setId('selectLabel');
  flow.add(label);
  var allfiles =  DocsList.getAllFiles();

  var verticalPanel = app.createVerticalPanel().setId('verticalPanel');

  for(var i=0;i<allfiles.length;i++)
  {
    var temp = app.createCheckBox(allfiles[i].getName()).setName('cb'+i).setId('cb'+i);
    var tempvalue = app.createHidden('cbvalue'+i, allfiles[i].getId());
    verticalPanel.add(temp);
    verticalPanel.add(tempvalue);
  }

  var scrollPanel = app.createScrollPanel().setId('scrollPanel');
  scrollPanel.add(verticalPanel);
  scrollPanel.setSize("400", "250")
  flow.add(scrollPanel);

  var buttonsubmit = app.createSubmitButton("Copy");
  flow.add(buttonsubmit);

  form.add(flow);
  app.add(form);

  SpreadsheetApp.getActiveSpreadsheet().show(app);
}

function doPost(eventInfo) {
  var app = UiApp.getActiveApplication();
  var allfiles =  DocsList.getAllFiles();
  var tempSsId = "";

  for(var i=0;i<allfiles.length;i++)
  {
    var temp = eventInfo.parameter['cb'+i];
    if(temp == 'on')
    {
      tempSsId = eventInfo.parameter['cbvalue'+i];
      var activeSheet = SpreadsheetApp.getActiveSheet().copyTo(SpreadsheetApp.openById(tempSsId));
      activeSheet.setName(SpreadsheetApp.getActiveSheet().getSheetName());
    }
  }

  var label = app.createLabel('statusLabel');
  label.setText("Copied Active sheet in all selected Spreadsheets...");
  label.setVisible(true);
  app.add(label);
  return app;
}

这是[http://igoogledrive.blogspot.ca/2012/10/Google-Spreadsheet-Script-to-Copy-Sheet-to-multiple-Spreadsheets-at-a-time.html][1]的原始代码,之前对我来说很好,但现在还没有。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

试试这个

function doGet()
{
  var app = UiApp.createApplication();
  app.setTitle("Kishan - Copy Sheet in Multiple Spreadsheets");

  var form = app.createFormPanel();
  var flow = app.createFlowPanel();

  var label = app.createLabel("Select Spreadsheet where you want to copy the current sheet:").setId('selectLabel');
  flow.add(label);
  var allfiles =  DriveApp.getFiles();

  var verticalPanel = app.createVerticalPanel().setId('verticalPanel');
  var i = 0;
  while (allfiles.hasNext())
  {
    var file = allfiles.next();
    var temp = app.createCheckBox(file.getName()).setName('cb'+i).setId('cb'+i);
    var tempvalue = app.createHidden('cbvalue'+i, file.getId());
    verticalPanel.add(temp);
    verticalPanel.add(tempvalue);
    i++;
  }

  var scrollPanel = app.createScrollPanel().setId('scrollPanel');
  scrollPanel.add(verticalPanel);
  scrollPanel.setSize("400", "250")
  flow.add(scrollPanel);

  var buttonsubmit = app.createSubmitButton("Copy");
  flow.add(buttonsubmit);

  form.add(flow);
  app.add(form);

  SpreadsheetApp.getActiveSpreadsheet().show(app);
}

function doPost(eventInfo) {
  var app = UiApp.getActiveApplication();
  var allfiles =  DriveApp.getFiles();
  var tempSsId = "";

  var i = 0;
  while (allfiles.hasNext())
  {
    var temp = eventInfo.parameter['cb'+i];
    if(temp == 'on')
    {
      tempSsId = eventInfo.parameter['cbvalue'+i];
      var activeSheet = SpreadsheetApp.getActiveSheet().copyTo(SpreadsheetApp.openById(tempSsId));
      activeSheet.setName(SpreadsheetApp.getActiveSheet().getSheetName());
    }
    i++;
  }

  var label = app.createLabel('statusLabel');
  label.setText("Copied Active sheet in all selected Spreadsheets...");
  label.setVisible(true);
  app.add(label);
  return app;
}
相关问题