[英]Dynamically Updating Formulas From Another Google Spreadsheet
I have two google spreadsheets, a master copy and a child copy. 我有两个谷歌电子表格,一个主副本和一个子副本。 When I update the formulas in the master copy, I want it to update the formulas in the child copy too.
当我更新主副本中的公式时,我希望它也更新子副本中的公式。 I intend to create many copies of the child copy and don't want to update the formulas in each one every time something small has to change.
我打算创建子拷贝的许多副本,并且不希望每次小的必须更改时更新每个副本中的公式。
I've tried using IMPORTRANGE(), and it only gives me the values, not the formulas. 我尝试过使用IMPORTRANGE(),它只给出了值,而不是公式。 So I thought to create my own script to get this done.
所以我想创建自己的脚本来完成这项工作。 I'm using the following code to attempt this:
我正在使用以下代码来尝试此操作:
function REMOTEDATA(inKey, inRange) {
var outData;
var ss = SpreadsheetApp.openById(inKey);
if (ss) {
outData = ss.getRange(inRange).getFormulas();
}
return outData;
}
//My manifest is the following:
{
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets"
],
"timeZone": "America/Denver",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER"
}
I'm very new to oAuth and I don't understand how to get this to work. 我对oAuth很新,我不明白如何让它工作。 When I attempt this it gives me the following:
当我尝试这个时,它给了我以下内容:
"You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets (line 4)." “您无权调用SpreadsheetApp.openById。所需权限: https : //www.googleapis.com/auth/spreadsheets (第4行)。”
Can anyone tell me what I need to add to get this to work? 任何人都可以告诉我需要添加什么来让这个工作? If there's an easier way, I'm all ears.
如果有一个更简单的方法,我会全力以赴。
EDIT: After reading Ruben's comment below, I changed the way I was doing it to having a button on the sheet itself run the function. 编辑:在阅读下面的Ruben评论之后,我改变了我的方式,让工作表上的按钮运行该功能。 It fixed the first problem, but now I'm running into another problem.
它解决了第一个问题,但现在我遇到了另一个问题。 I can't copy all the data directly from one sheet to another.
我不能将所有数据直接从一张纸复制到另一张。 I'm trying to get formulas, values, and formatting.
我正在尝试获取公式,值和格式。 If I just use getFormulas() and setFormulas() with the ranges, it gets rid of all the values that aren't formulas, and it doesn't change the formatting.
如果我只使用带有范围的getFormulas()和setFormulas(),它将删除所有非公式的值,并且不会更改格式。 I've been trying to use copyTo() but it tells me I can't do this between spreadsheets.
我一直在尝试使用copyTo(),但它告诉我,我不能在电子表格之间做到这一点。 My current code is:
我目前的代码是:
function REMOTEDATA() {
var MASTERKEY = "key";
var UPDATERANGES = ["A1:B200", "C7:C200", "D1:H200", "J1:L200", "N1:S200", "U1:X200", "z1:ac200", "AE1:AH200", "AJ1:AL200"];
var ss = SpreadsheetApp.openById(MASTERKEY);
var cur_ss = SpreadsheetApp.getActiveSpreadsheet();
UPDATERANGES.forEach(function(range) {
data = ss.getRange(range).copyTo(cur_ss.getRange(range));
});
}
My head is starting to hurt looking at the different methods for the Ranges Class. 看着Ranges Class的不同方法,我的头开始受伤了。 I can't see any methods to do this, but I feel like I'm missing something.
我看不到任何方法可以做到这一点,但我觉得我错过了一些东西。 Any other ideas?
还有其他想法吗?
EDIT 3: Okay. 编辑3:好的。 I figured out a workaround.
我想出了一个解决方法。 Basically what I did was copy the entire sheet from one spreadsheet to the other using the sheet.copyTo() function.
基本上我所做的是使用sheet.copyTo()函数将整个工作表从一个电子表格复制到另一个电子表格。 Then after that I used the range.copyTo() to get what I wanted.
之后我使用range.copyTo()来获得我想要的东西。 Then I deleted the copied sheet using sheet.deleteSheet()
然后我使用sheet.deleteSheet()删除了复制的工作表
Custom functions run anonymously so they can't call services that require authorization, like openById
. 自定义函数匿名运行,因此无法调用需要授权的服务,如
openById
。
Instead of using a custom function to execute your script use another method like a custom menu, assign the script to an image, etc. or just run it from the script editor. 而不是使用自定义函数来执行您的脚本使用另一种方法,如自定义菜单,将脚本分配给图像等,或者只是从脚本编辑器运行它。
NOTE: You will have to modify your script a bit or use a another function to pass the required arguments. 注意:您必须稍微修改脚本或使用另一个函数来传递所需的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.