使用Apps脚本代码更新(覆盖)一个Apps脚本文件与另一个Apps脚本文件 [英] Update (Overwrite) one Apps Script file with another Apps Script file using Apps Script Code

查看:84
本文介绍了使用Apps脚本代码更新(覆盖)一个Apps脚本文件与另一个Apps脚本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

覆盖文件。覆盖应用程序脚本文件。



这是不是创建新的Apps脚本文件的问题。这不会帮助我。我需要 更新 现有的Apps脚本文件。这个问题类似于创建一个新文件,但这不是同一个问题。更新的语法和更新的要求可能与创建新文件有很大不同,我无法从有关创建新文件的答案中获得解决方案。我已经看过创建一个新文件的答案,但还没有回答我的问题。



我尝试使用Advanced Drive Service来更新现有的Apps脚本文件与另一个Apps脚本文件。

  function updateMyScript(){
var targetFileID ='File ID';

var dataSource = {
files:[
{
id:739a57da-f77c-4e1a-96df-7d86ef227d62,
name:Code,
type:server_js,
source:function doGet(){\\\
return HtmlService.createHtmlOutputFromFile(\\\'index\\\'); \\\
} \\\

},
{
id:2c7e8b5a-dbc5-4cd2-80e9-77b6291b3167,
name:index ,
type:html,
source:\\\\\\
\\\\\\
新消息!! \\\
\\\\\\
\\\

}
]
};

var filesResource = Drive.Files.get(targetFileID);
var blob = Utilities.newBlob(JSON.stringify(dataSource),application / vnd.google-apps.script + json);
var whatRezult = Drive.Files.update(filesResource,targetFileID,blob,{convert:true});

Logger.log('whatRezult:'+ whatRezult);
};

id $ c> dataSource object是每个特定的 .gs html 文件的id该项目。通过将目标Apps脚本文件下载到JSON,然后打开JSON文件并复制文件ID,我得到了这些ID。所以,我知道那些是正确的。我想用代码从项目中检索这些单独的文件ID。关于创建 应用程序脚本文件的文章并未解释如何从项目文件中获取子ID。项目文件ID与项目内部的每个文件ID不同。 名称类型属性很明显。从这个例子中只有两种类型的文件具有 server_js 和html类型。对于Apps Script项目文件的内部文件,它看起来像可以是字符串文字。所以,你可以输入任何你想要的替换内容。



目标ID 不言自明。



如果有方法可以使用Advanced Drive Service或 UrlFetchApp.fetch()会回答我的问题。我只想使用Apps脚本。所以,我不想寻找用其他语言编写的解决方案,或者是从Apps脚本以外运行的解决方案。



通过上面的代码,我得到

pre $ Drive $ File $更新真正});

错误是:


该应用程序不具备更新Apps脚本所需的范围。


显然,它正在寻找一个范围设置在某处显示。我猜测它会进入选项的第四个参数。

解决方案

您需要询问特殊的Drive -Apps脚本范围

  https://www.googleapis.com/auth/drive.scripts 

由于您无法告诉Apps Script向您询问此范围(它通过分析您的代码自动确定其范围)。你需要自己做舞动(例如使用 Eric's lib )。但是,既然你也不能设置这个令牌,你自己检索脚本以便在其内置或高级服务调用中使用(不是我所知道的),你还必须手动执行UrlFetch调用,并将你的标题中的自定义标记(如创建新脚本的问题。



update 的UrlFetch调用非常类似于 insert one,只需将该方法更改为 PUT ,并在路径中添加apps脚本项目id。 / p>

  var url =https://www.googleapis.com/upload/drive/v2/ files /+ scriptID; 
var requestBody = ...; //同样的
var options = {
headers:{
'Authorization':'Bearer'+ yourManuallyFetchedToken,
},
contentType:application / vnd.google-apps.script + json,
hod:PUT,//在这里从POST更改为PUT
payload:JSON.stringify(requestBody)
}


Overwrite file. Overwrite Apps Script file.

This is not a question to create a new Apps Script file. That won't help me. I need to update an existing Apps Script file. This question is similar to creating a new file, but it's not the same issue. The syntax for an update, and the requirements for an update may be different enough from creating a new file, that I can't get a solution from the answer about creating a new file. I've looked at an answer for creating a new file, and that has not answered my question.

I have tried using the Advanced Drive Service to update an existing Apps Script file with another Apps Script file.

function updateMyScript() {
  var targetFileID = 'File ID';

  var dataSource = {
    "files": [
      {
        "id":"739a57da-f77c-4e1a-96df-7d86ef227d62",
        "name":"Code",
        "type":"server_js",
        "source":"function doGet() {\n  return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
      },
      {
        "id":"2c7e8b5a-dbc5-4cd2-80e9-77b6291b3167",
        "name":"index",
        "type":"html",
        "source":"\u003chtml\u003e\n  \u003cbody\u003e\n    New message!!\n  \u003c/body\u003e\n\u003c/html\u003e"
      }
    ]
  };

  var filesResource = Drive.Files.get(targetFileID);
  var blob = Utilities.newBlob(JSON.stringify(dataSource), "application/vnd.google-apps.script+json");
  var whatRezult = Drive.Files.update(filesResource, targetFileID, blob, {"convert":"true"});

  Logger.log('whatRezult: ' + whatRezult);
};

The id properties in the dataSource object are id's of each specific .gs or html file inside of the project. I got those id's by downloading the "target" Apps Script file to JSON, then opening up the JSON file and copying out the file id's. So, I know that those are correct. I'd like a way to retrieve those individual file ID's from the project with code. The post about creating a new apps script file, does not explain how to get the "sub" ID's from the project file. The project file ID is not the same as each file ID inside of the project. The name and type properties are obvious. And there are only two types of files which, from the example, have types of server_js and "html". It looks like the source for the internal files to the Apps Script project file can be a string literal. So, you can just type in whatever you want the replacement content to be.

The target ID is self explanatory.

If there is a way to do this with either the Advanced Drive Service or UrlFetchApp.fetch() that will answer my question. I only want to use Apps Script. So, I'm not looking for a solution written in some other language, or that is run from outside of Apps Script.

With the above code, I'm getting an error from the next to last line:

Drive.Files.update(filesResource, targetFileID, blob, {"convert":"true"});

The error is:

The app does not have the required scope to update Apps Scripts.

So, obviously, it's looking for a scope setting to be indicated somewhere. I'm guessing that it would go into the the fourth parameter for the options.

解决方案

You need to ask for the special Drive-AppsScript scope:

https://www.googleapis.com/auth/drive.scripts

Since you cannot tell Apps Script to ask this scope for you (it determines its own scopes automatically by analyzing your code). You need to do the oAuth dance yourself (by using Eric's lib for example). But then since you also cannot set this token you retrieved yourself for the script to use in its built-in or advanced service calls (not that I know of anyway), you'll have to do the UrlFetch call manually too, and pass your "custom" token in the header (like shown in the "create new script" question.

The UrlFetch call to update is very similar to the insert one. Just change the method to PUT and add the apps script project id in the path.

var url = "https://www.googleapis.com/upload/drive/v2/files/" + scriptID;
var requestBody = ...; //the same
var options = {
  "headers": {
     'Authorization': 'Bearer ' +  yourManuallyFetchedToken,
   }, 
  "contentType": "application/vnd.google-apps.script+json",
  "method" : "PUT", //changed here from POST to PUT
  "payload": JSON.stringify(requestBody)
}

这篇关于使用Apps脚本代码更新(覆盖)一个Apps脚本文件与另一个Apps脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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