google-apps-script - 从 GetRangeByName 获取范围,激活范围
问题描述
我的目标是自动将 10 行添加到非营利组织的业务使用的 Google 表格中,然后复制新创建的行中所需的公式序列。我让所有代码都能完成任务,并防止用户在手动插入行时弄乱电子表格公式。但是,由于循环使用 getRange() 的电子表格中的行数,代码会超时。我的新方法是以跳转到命名单元格作为起点,而不是真正缓慢的循环单元格搜索。
我创建了一个名称“EndData”,阅读了我可以在网上找到的所有内容,尝试了几个小时的语法错误以将 named_cell 范围放入 myrange,然后激活工作表上的范围......
这是当前的编码尝试(将光标留在列的顶部,并且
“TypeError:在对象表中找不到函数 getRangeByName。(第 170 行,文件“宏”)”
//Get EOD range, select, index up 3 rows to start row insertions
function getEOD() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
Logger.log(ss); //lOG is not helpful, says, "sheet", not SheetName
var MyRange = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRangeByName("EndData");
Logger.log(MyRange); //lOG is not helpful, says, "Range", not RangeAddress
//Activate the named cell, moves with the spreadsheet
MyRange.activate();
};
在我寻求帮助后有了一个新想法,这是完成工作的工作代码:
//Get EOD range, select, index up 3 rows to start row insertions
function getEOD() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//Logger.log(ss); //lOG is not helpful, says, "sheet", not SheetName
var rg = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('EndData');
if (rg != null) {
Logger.log(rg.getNumColumns());
}
//Logger.log(rg); //lOG is not helpful, says, "Range", not RangeAddress
//Referenced the Named the EOD cell
//Future, Trying to create a debug status bar
//SpreadsheetApp.getActiveSpreadsheet().toast("looking at row" & i "now")
//Activate the named cell, which moves with spreadsheet growth, down
rg.activate();
//Uncomment for testing purposes, places a Yes on the row 4 columns over
//sheet.setActiveRange(sheet.getRange(0, 4)).setValue('Yes');
//turned off durning testing, writes in data range with this trial code
//Reposition from named cell to insert lines location
ss.getRange(sheet.getCurrentCell().getRow() -2, 1, 1,
sheet.getMaxColumns()).activate();
//Insert ten lines, copy and paste special formulas only
Insert_10_Lines()
//https://stackoverflow.com/questions/59772934/get-range-from-
getrangebyname-activate-range
};
解决方案
我的回答是我有毅力,我尝试一次限制一个变量以证明事情的真正运作方式,将在测试脚本文件中有效的代码移动到生产脚本文件中,但它不适用于复制什么我在测试中,回到测试脚本并且它也没有工作,在它之后......在脚本,储蓄和运行步骤中似乎发生了一些变量,这些变量改变了我的交互式响应得到。通过生产环境的反复试验,通过转到建议的简单代码并获得一个有效的组合,得出了一个有效的组合。这是产品中运行的代码,以满足我的问题的主要需求...
function InsertRows() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//Get range from named cell
var range = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("EOD");
if (range != null) {
Logger.log(range.getNumColumns());
}
//Select the cell as a starting point for code to follow
range.activate(); //start location cell is active and row is selected
感谢那些响应的帮助!看起来需要一段时间才能识别模式并弄清楚如何及时获得一致的结果......
推荐阅读
- javascript - 如何在 Highcharter 中使用 JavaScript?
- javascript - 为什么使用 React Hooks、MomentJS 对象和间隔/超时更新状态不会触发重新渲染?
- mustache - 如何修复以'm_-'开头的随机字符串,在用小胡子渲染模板后附加到类
- android - 我可以在 Android 的混合应用程序中(通过 Cordova)使用 navigator.mediaDevices.getUserMedia 来使用相机吗?
- c++ - 如何使用 llvm 框架执行函数参数寄存器分配?
- powerbi - 获取所有设备,无论是否有最后借用它的用户
- c# - 优化复杂对象比较
- angular - Highcharts不显示数据
- java - 在 main() 方法中通过子对象调用覆盖的方法
- php - 如果选择来自数据库的下拉菜单,则将数据插入输入值