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

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

问题描述

Google文档中没有分享一张电子表格的方法。所以,你必须共享一个电子表格。所以,我正在想写一个脚本来同步两张纸(每张纸在不同的电子表格中)。我想用一个函数来获取数组的行来做到这一点。有没有更好的策略来做到这一点?

解决方案

您可以通过在两个电子表格中添加一个脚本它是更改触发器上其他电子表格的内容。例如,如果您要将以下内容添加到这两个电子表格中,请交换源和目标信息。

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

函数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());

$ / code>

只需为importData函数添加更改触发器,然后进行任何更改要么将文件复制到其他电子表格,从而保持两个同步。

显然,如果两张电子表格都在同一时间更新,您将遇到麻烦。

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()); 
}

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天全站免登陆