Google Apps脚本插入行代码不再使用保护集

时间:2015-07-06 14:59:52

标签: google-apps-script

我已经使用了以下代码4年了,没有任何问题。但是,随着Google新版本的介绍,我收到了以下错误:

遇到错误:服务错误:电子表格

我应该补充说,除了用户可以插入行的行范围之外,我已经保护了整个工作表。其他地方都受到保护。

如果工作表不受保护,该功能可以100%工作。

这是我的代码:

//INSERT ROWS

//Function doGet(): Creates and sets up a User Interface (UI) with a pop-up box for user to enter dersired number of rows to insert.

function doGet(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var app =UiApp.createApplication().setTitle('Insert Rows').setHeight(120).setWidth(250);

  // Create a grid with text box and corresponding labels. 
  // Text entered into the text box is passed in to numRows.
  // The setName extension will make the widget available by the given name to the server handlers later.

  var grid = app.createGrid(3, 2);
  grid.setWidget(0, 0, app.createLabel('Number of Rows to Insert:'));
  grid.setWidget(0, 1, app.createTextBox().setName('numRows').setFocus(true).setWidth(50));
  grid.setWidget(1, 0, app.createLabel('Maximum 1000 Rows'));
  grid.setWidget(2, 0, app.createLabel(''));

  // Create a Vertical Panel and add the Grid to the Panel.

  var panel = app.createVerticalPanel();
  panel.add(grid);

  // Create a button and Click Handler.
  // Pass in the Grid Object as a callback element and the handler as a click handler.
  // Identify the function insertRows as the server click handler.

  var button = app.createButton('Insert Rows');
  var handler = app.createServerHandler('insertRows');
  handler.addCallbackElement(grid);
  button.addClickHandler(handler);

  // Add the button to the Panel, add Panel to the App, launch the App

  panel.add(button);
  app.add(panel);
  ss.show(app);
}

//Function insertRows(e): Makes the current Spreadsheet and Sheet active.
//Fetches the number of rows from the UI.
//Determines the current cursor position.
//Defines the hidden column range.
//Unhides the protected columns.
//Inserts the desired number of rows below the current cursor position.

function insertRows(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cursPos = sheet.getActiveCell().getRow();
  var valueRows = e.parameter.numRows;
  var lastColumn = sheet.getLastColumn();
  sheet.insertRowsAfter(cursPos, valueRows);

//Defines the source range for data to be copied to the newly inserted rows.
//Defnies the target range for where the source data is to be copied.
//Copies the source data into the target data range.
//Hides the protected columns.

  var source_range = sheet.getRange(cursPos,1,1,lastColumn);
  var target_range = sheet.getRange(cursPos+1,1,valueRows);
  source_range.copyTo(target_range);

//Closes the UI application and hands the spreadsheet back to the user.

  SpreadsheetApp.getActiveSpreadsheet().toast("...Completed", "Insert Rows:", 3);

  var app = UiApp.getActiveApplication();
  app.close();
  return app;
}

三江源。

1 个答案:

答案 0 :(得分:0)

createGrid已被弃用,您需要切换到HTMLService。

https://developers.google.com/apps-script/reference/ui/grid