当源工作表添加了行时,如何编写在一个工作表中添加行的脚本
How to script the addition of a row in one sheet when the source sheet has a row added?
我有一个谷歌表单,它填充了一个谷歌电子表格(源表)。我找到并自定义了一个脚本,将某些列(全部)从源工作表拉到新的选项卡/工作表中。显然,两张表中的行数必须相同,脚本才能正常运行。如果没有,则返回错误。每次提交新表单时,都会在源工作表中添加一行,从而使两个工作表不同步并破坏脚本。
我想帮助弄清楚我需要向现有脚本添加什么功能(或者我可以对它进行什么更改),这样当源工作表中出现新行时(因为表单已经提交),目标工作表中就会出现一个空行。
我需要调整我的脚本还是添加一个新函数?
function importFunction(a) {
var ss = SpreadsheetApp.openById("0ApTaY3v27-UqdElwZTBvanNpaC1UckpxaTJRZS1XNWc");
var sourceSheet = ss.getSheets()[0];
var ss2 = SpreadsheetApp.openById("0ApTaY3v27-UqdElwZTBvanNpaC1UckpxaTJRZS1XNWc");
var targetSheet = ss2.getSheets()[1];
var values1 = sourceSheet.setActiveSelection("C:C").getValues();
var values2 = sourceSheet.setActiveSelection("AD:AD").getValues();
var values3 = sourceSheet.setActiveSelection("D:E").getValues();
var values4 = sourceSheet.setActiveSelection("AE:AE").getValues();
var values5 = sourceSheet.setActiveSelection("F:H").getValues();
var values6 = sourceSheet.setActiveSelection("N:U").getValues();
targetSheet.setActiveSelection("A:A").setValues(values1);
targetSheet.setActiveSelection("B:B").setValues(values2);
targetSheet.setActiveSelection("C:D").setValues(values3);
targetSheet.setActiveSelection("E:E").setValues(values4);
targetSheet.setActiveSelection("F:H").setValues(values5);
targetSheet.setActiveSelection("I:P").setValues(values6);
}
根据下面的建议,我试图将脚本更改为以下内容,但我遇到了一个错误-找不到方法appendRow()。我该怎么解决?
function importFunction(a) {
var ss = SpreadsheetApp.openById("0ApTaY3v27-UqdElwZTBvanNpaC1UckpxaTJRZS1XNWc");
var sourceSheet = ss.getSheets()[0];
var ss2 = SpreadsheetApp.openById("0ApTaY3v27-UqdElwZTBvanNpaC1UckpxaTJRZS1XNWc");
var targetSheet = ss2.getSheets()[1];
var targetMax = targetSheet.getMaxRows();
var values1 = sourceSheet.setActiveSelection("C:C").getValues();
var values2 = sourceSheet.setActiveSelection("AD:AD").getValues();
var values3 = sourceSheet.setActiveSelection("D:E").getValues();
var values4 = sourceSheet.setActiveSelection("AE:AE").getValues();
var values5 = sourceSheet.setActiveSelection("F:H").getValues();
var values6 = sourceSheet.setActiveSelection("N:U").getValues();
if(targetMax == values1.length) {
targetSheet.setActiveSelection("A:A").setValues(values1);
targetSheet.setActiveSelection("B:B").setValues(values2);
targetSheet.setActiveSelection("C:D").setValues(values3);
targetSheet.setActiveSelection("E:E").setValues(values4);
targetSheet.setActiveSelection("F:H").setValues(values5);
targetSheet.setActiveSelection("I:P").setValues(values6);
}
else
targetSheet.appendRow();
}
在使用getMaxRows()写入值之前,您可以简单地检查工作表SS2中的可用行数,并将其与数据数组value1.length
的长度进行比较,然后,根据此比较,最终在for next循环中使用appendRow()添加所需的行。
EDIT:在您的EDIT之后,我进行了进一步的测试,appendRow()
似乎没有像我想象的那样工作,它在空表的开头添加了Rows。。。所以我尝试了这个:
if(targetMax < values1.length) {
var RowsToAdd = values1.length-targetMax
for(nn=0;nn<RowsToAdd;++nn){targetSheet.insertRowAfter(targetMax)}
}
只需将其放置在var value6=...
之后
对这个小错误感到抱歉;-)
相关文章:
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- jQuery函数不能只在一个页面上工作
- jQuery 1.7.2:.on()在一个页面上工作;Don’别再干别的了
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- 为什么这个jQuery可以作为一个小提琴工作,但在我的网页上没有
- DIV怎么能像Javascript中的另一个元素一样工作呢
- 我需要一个jQuery函数来只工作在700px以上的屏幕大小,无法在我的代码中发现错误
- 如何获得一个与用“”声明的变量工作方式相同的变量;设“;或在with块中
- 使用几个<脚本>标签不会't工作-只调用一个脚本
- 给出<选项>标记一个类?API调用不工作
- 当单元格内容由于forumula从另一个工作表编译数据而更改时发送电子邮件
- 我做了一个jquery幻灯片,但没有;我工作不好,我该怎么办
- 当阻止Enter键提交AJAX表单时,关注下一个输入是't工作
- 代理一个网站能够在iframe中进行所见即所得编辑-它是如何工作的
- 同一组JS只工作一个,而其他人不工作在IE上
- 隐藏/显示功能一次只工作一个?——Jquery
- 从D3.js v3迁移到D3.js v4不工作-一个选择问题
- 在实现文件中只工作一个函数
- 相同的jquery插件在相同的url只工作一个