[英]Copy data from one spreadsheet to another on edit
I already try everthing but I cant make this work =/我已经尝试了一切,但我无法完成这项工作=/
I'm trying to copy data from one spreadsheet to other using setValues(), because link doesn't work for me.我正在尝试使用 setValues() 将数据从一个电子表格复制到另一个电子表格,因为链接对我不起作用。 I also need to keep the trigger on edit.
我还需要保持触发器处于编辑状态。
So, I create one fuction called AddConvocacao, and always when have any change the script run.因此,我创建了一个名为 AddConvocacao 的函数,并且总是在有任何更改时运行脚本。
function addConvocacao(e){
var linha = e.range.getRow(); //get the number of the line, where have edit
var nome = e.source.getActiveSheet().getRange(linha,2).getValue(); //get the value of the field
var targetSheet = SpreadsheetApp.openById('1iQbZ7iB9ddc-HwLK9vG0eVEeWWqXKJZ0ry7U_Hm4SkQ') //open the other spreedsheet
var targetName = targetSheet.getSheetByName('Pac'); //open the tab
var lastRow = targetName.getLastRow(); //count the last row in this tab
lastRow = lastRow+1; //add 1
targetName.getRange(lastRow, 2).setValue(nome); // set the value
// targetName.getRange(lastRow, 3).setValue(valorCol7);
// targetName.getRange(lastRow, 5).setValue(dose);
// targetName.getRange(lastRow, 6).setValue(diagnostico);
// targetName.getRange(lastRow, 7).setValue(medico);
}
why dosen't work when I use on edit?为什么我在编辑上使用时不起作用?
Thanks so much!非常感谢! =)
=)
I did it a little different I think.我觉得我做的有点不同。 I put checkboxes in column one and capture the value from column 2 and sent it to the other spreadsheet to the bottom of column 2
我将复选框放在第一列并从第 2 列捕获值并将其发送到另一个电子表格到第 2 列的底部
function onMyEdit(e) {
//e.source.toast('entry');
const sh = e.range.getSheet();
//Logger.log(JSON.stringify(e));
if (sh.getName() == 'Sheet1' && e.range.columnStart==1 && e.value=="TRUE") {
//e.source.toast('cond');
const v = sh.getRange(e.range.rowStart, 2).getValue();
var tss = SpreadsheetApp.openById(getGlobal('targetid'));
var tsh = tss.getSheetByName('Sheet1');
tsh.getRange(tsh.getLastRow() + 1, 2).setValue(v);
e.range.setValue("FALSE");
}
}
You must use an installable trigger to us openById();您必须对我们使用可安装的触发器 openById();
getGlobal(targetid) just gets a spreadsheet id you can replace that with the id getGlobal(targetid) 只是获取一个电子表格 id,您可以将其替换为 id
Also I limited the action to only one sheet and only when I set the checkbox to true.此外,我将操作限制为仅一张纸,并且仅当我将复选框设置为 true 时。 I reset it back to false in the code.
我在代码中将其重置为 false。
You'll need to modify a few things to get it to work for you because when I'm doing the examples I do it my way.你需要修改一些东西才能让它为你工作,因为当我做例子时,我会按照我的方式去做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.