[英]Google script, how to access and paste on edit to a specific sheet within another spreadsheet?
I am attempting to create a script so that when a change is made in one spreadsheet the corresponding sheet with the same name in another spreadsheet receives the same change in the same place, and so I can put one lot of the script in each one and get two linked sheets that affect each other. 我试图创建一个脚本,以便在一个电子表格中进行更改时,另一电子表格中具有相同名称的相应工作表在同一位置收到相同的更改,因此我可以在每个脚本中放置很多脚本,然后获得两个相互影响的链接表。
this is the code: 这是代码:
var targetID = 'ID of the sheet';
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
function onEdit(e){
var range = e.range;
var value = e.value;
var row = range.getRowIndex();
var column = range.getColumnIndex();
exportValue(row,column,value)
}
function exportValue(row,column,value) {
//(**this is the point where it breaks**)
var s = SpreadsheetApp.openById(targetID).getSheetByName(targetSheet);
var target = s.getRange(row, column);
target.setValue(value);
}
In my version i put in logs in between each line to see where it failed and it got to the line where i have put " this is the point where it breaks " and then didn't return anything after that. 在我的版本中,我在每行之间插入日志,以查看失败的地方,然后到达我已输入“ 这是中断点 ”的行,然后在此之后未返回任何内容。
Since attempting this I tried to open up the other file by just pulling out all of the variables but I couldn't get it to work. 自从尝试这样做以来,我试图通过拉出所有变量来打开另一个文件,但是我无法使其正常工作。
the current error messages that it is going with are: 当前要处理的错误消息是:
Cannot find function getSheetByName in object Sheet
在对象Sheet中找不到函数getSheetByName
Cannot find function openById in object Spreadsheet
在对象Spreadsheet中找不到函数openById
I have spent so much time on this already and I feel like the answer is really simple but I would really appreciate any advice 我已经花了很多时间,我觉得答案很简单,但是我非常感谢任何建议
Thanks in advance 提前致谢
:) :)
I got this to work by using an installable onEdit(e) Trigger. 我通过使用可安装的onEdit(e)触发器来实现这一点。
function Linked1Edit(e){
var ss=SpreadsheetApp.openById('The Other Spreadsheets ID');
var sh=ss.getSheetByName(e.range.getSheet().getName());
var rg=sh.getRange(e.range.rowStart,e.range.columnStart);
rg.setValue(e.value);
}
I called one Linked1Edit(e) and the other Linked2Edit(e) and created an installable onEdit(e) trigger for each one and now they write to each other. 我分别调用了一个Linked1Edit(e)和另一个Linked2Edit(e),并为每个对象创建了一个可安装的onEdit(e)触发器,现在它们可以互相写入。 Unfortunately, it only works for single valued changes though.
不幸的是,它只适用于单值变更。
The following script will allow you to make changes to more than one value at a time. 以下脚本允许您一次更改多个值。
function Linked1Edit(e){
var ss=SpreadsheetApp.openById('The Other Spreadsheet ID');
var sh=ss.getSheetByName(e.range.getSheet().getName());
var rg=sh.getRange(e.range.rowStart,e.range.columnStart,e.range.rowEnd-e.range.rowStart+1,e.range.columnEnd-e.range.columnStart+1);
var vA=e.range.getValues();
rg.setValues(vA);
}
Creating a Trigger 创建触发器
Select Current Project's Triggers 选择当前项目的触发器
Click Add Trigger: 单击添加触发器:
Create Trigger Dialog: 创建触发器对话框:
You can also create a trigger in code. 您还可以在代码中创建触发器。 See ScriptApp Class
请参阅ScriptApp类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.