简体   繁体   English

Google脚本,如何访问并粘贴编辑到另一个电子表格中的特定工作表?

[英]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 提前致谢

:) :)

Linked Spreadsheets with Installable onEdit() triggers 具有可安装的onEdit()触发器的链接电子表格

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.

相关问题 如何将 Google 电子表格的特定表格和 CSV 值保存在同一电子表格中另一表格的单个单元格中? - How to save a specific sheet of Google Spreadsheet a CSV values in a single cell of another sheet within the same Spreadsheet? 如何使用谷歌应用脚本在特定范围内从工作表中获取特定图表并将其粘贴到另一张工作表上? - How to get a specific chart from a sheet and paste it on another sheet, in a specific range using google apps script? 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 如何将过滤器本身复制到同一个谷歌电子表格中的另一个工作表? - How to copy the filter itself to another sheet within the same google spreadsheet? 如何制作一个按钮以跳到Google Spreadsheet中另一个工作表中的特定单元格? - How to make a button to jump to a specific cell in another sheet in Google Spreadsheet? 如何使用Google脚本将详细信息表复制到另一个电子表格 - How can copy specifics sheet to another spreadsheet using google script Google Script:如何将CopyRows从一个工作表复制到另一个工作表 - Google Script: How to CopyRows from one sheet to another spreadsheet 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 使用谷歌工作表脚本时,如何将电子表格的工作表复制到另一个电子表格中而不创建新的工作表副本? - How to copy spreadsheet's sheet into another spreadsheet without creating a new sheet copy when using google sheet script? 您如何仅向 Google 电子表格中的工作表所有者授予编辑权限? - How do you give edit access only to the owner of the sheet in Google spreadsheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM