有没有办法让两张纸保持同步? [英] Is there a way to keep two sheets synchronized?

查看:28
本文介绍了有没有办法让两张纸保持同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有办法在 Google 文档中只共享一个电子表格中的一张.因此,您必须共享整个电子表格.所以,我正在考虑编写一个脚本来同步两张工作表(每个工作表都在不同的电子表格中).我想使用一个函数来获取行作为数组来做到这一点.有没有更好的策略来做到这一点?

There isn't a way to share only one sheet of one spreadsheet in Google Docs. So, you have to share an entire spreadsheet. So, I was thinking in writing a script to synchronize two sheets (each one in a different spreadsheet). I thought using a function to get rows as array to do this. Is there a better strategy to do that?

推荐答案

实现此目的的一种方法是将脚本添加到两个电子表格中,以便在更改触发器上将其内容复制到另一个电子表格中.例如,如果您要在两个电子表格中添加如下内容,交换源信息和目标信息.

One way you could accomplish this is by adding a script to both spreadsheets that copies it's contents to the other spreadsheet on a change trigger. For example if you were to add something like the below to both spreadsheets, swapping the source and destination information around.

var sourceSpreadsheetID = "ID HERE";
var sourceWorksheetName = "SHEET NAME HERE";
var destinationSpreadsheetID = "ID HERE";
var destinationWorksheetName = "SHEET NAME HERE";

function importData() {
  var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
  var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
  var thisData = thisWorksheet.getDataRange();
  var toSpreadsheet = SpreadsheetApp.openById(destinationSpreadsheetID);
  var toWorksheet = toSpreadsheet.getSheetByName(destinationWorksheetName);
  var toRange = toWorksheet.getRange(1, 1, thisData.getNumRows(), thisData.getNumColumns())
  toRange.setValues(thisData.getValues()); 
}

只需为 importData 函数添​​加一个更改触发器,然后当对任一文档进行任何更改时,它会将内容复制到另一个电子表格,从而保持两者同步.

Just add a change trigger for the importData function and then when any changes are made to either document it will copy the contents to the other spreadsheet, thus keeping the both synced.

显然,如果两个电子表格同时更新,您就会遇到麻烦.

Obviously if both spreadsheets are being updated at the same time you will run into trouble.

这篇关于有没有办法让两张纸保持同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆