javascript Google 电子表格:插入行并复制隐藏的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16143705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:25:32  来源:igfitidea点击:

Google Spreadsheet: Insert Rows and copy down hidden content

javascriptgoogle-apps-script

提问by Mike Eburne

I have a Google Spreadsheet designed to calculate a balanced scorecard for businesses measuring employee demographics. The spreadsheet consists of several data entry cells and a number of 'hidden' columns containing specific formulae that calulates the data entered and produces a score.

我有一个 Google 电子表格,旨在为衡量员工人口统计数据的企业计算平衡计分卡。电子表格由几个数据输入单元格和许多“隐藏”列组成,这些列包含计算输入数据并产生分数的特定公式。

I need a script that will at any row in the spreadsheet (current cursor position): a) Script to be called from the main menu (Insert Rows) b) Insert a User defined number of rows below the current cursor position (pop up UI box requesting number of rows to insert) c) Copy down the data from the row above, including all data/formulae contained in the hidden columns d) Re-hide the protected columns and then hand back to the user.

我需要一个脚本,该脚本将位于电子表格中的任何行(当前光标位置):a)从主菜单(插入行)调用的脚本 b)在当前光标位置下方插入用户定义的行数(弹出 UI请求插入行数的框 c) 从上面的行复制数据,包括隐藏列中包含的所有数据/公式 d) 重新隐藏受保护的列,然后交还给用户。

The hidden columns contain IP that I do not want the users to see, hence the hidden/protected aspect.

隐藏列包含我不希望用户看到的 IP,因此是隐藏/受保护的方面。

Can anyone help?

任何人都可以帮忙吗?

My script so far...

到目前为止,我的脚本...

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [{name: "Insert Rows", functionName: "doGet"}];
  ss.addMenu("User Functions", menuEntries);
}

function doGet(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var app =UiApp.createApplication().setTitle('Insert Rows').setHeight(75).setWidth(225);
  // Create a grid with 1 text box and corresponding label. 
  // Test 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(1, 2);
  grid.setWidget(0, 0, app.createLabel('Number of Rows to Insert:'));
  grid.setWidget(0, 1, app.createTextBox().setName('numRows').setWidth(50));
  // 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('Submit');
  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) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cursPos = sheet.getActiveCell().getRow();
  var valueRows = e.parameter.numRows;
  sheet.insertRowsAfter(cursPos, valueRows);
  var app = UiApp.getActiveApplication();
  app.close();
  return app;
}

I now need to copy down the contents of the row above the current active cell (the top lh cell of the newly created rows) into the newly created rows.

我现在需要将当前活动单元格(新创建行的顶部 lh 单元格)上方行的内容复制到新创建的行中。

回答by miturbe

The following are apps-script "ingredients" that you can use to accomplish your needs:

以下是可用于满足您的需求的应用程序脚本“成分”:

A) Creates a new menu in the Spreadsheet UI. http://goo.gl/qCPRC

A) 在电子表格 UI 中创建一个新菜单。http://goo.gl/qCPRC

B1) SpreadSheet getActiveCell http://goo.gl/1wiBp

B1) 电子表格 getActiveCell http://goo.gl/1wiBp

B2) Displaying a User Interface from a Spreadsheet http://goo.gl/nL4y4

B2) 从电子表格http://goo.gl/nL4y4显示用户界面

C) Copy the content of the range to the given location. http://goo.gl/WRfcc

C) 将范围的内容复制到给定位置。http://goo.gl/WRfcc

D) No need to "unhide" the data is still available to read/copy. You do realize that hiding is no security at all since the user can "unhide" from the data menu.

D) 无需“取消隐藏”数据仍可读取/复制。您确实意识到隐藏根本没有安全性,因为用户可以从数据菜单中“取消隐藏”。

Good luck!

祝你好运!