Update (overwrite) one Script application file with another Script application file using the Script application Code

Overwrite file. Overwrite Script applications.

It is not a question of creating a new Script application. This will not help me. I need to update an existing Script application file. This question is similar to creating a new file, but this is not the same problem. The syntax of the update, and the requirements for updating can be quite different from creating a new file, and I can not get a solution from the answer to create a new file. I looked at the answer to creating a new file and did not answer my question.

I tried to use the Advanced Drive Service to upgrade an existing Script application 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);
};

id dataSource .gs html . , "target" Apps Script JSON, JSON . , , . . Script , "sub" . . name type . , , , server_js "html". , source Script . , , , .

target ID .

Advanced Drive UrlFetchApp.fetch(), . Script. , , - , Script.

:

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

:

.

, , - . , .

+4
3

Drive-AppsScript scope:

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

Apps Script ( , ). oAuth ( Eric lib, ). , , Script ( ), UrlFetch, "" ( script.

UrlFetch update insert. PUT id Script .

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)
}
+4

GitHub , Script () Script. .

GitHub - apps- script -update

. Read Me GitHub

GitHub API Script, .

+3

API . . (Sheet, Form, Doc) REST API PUT UrlFetchApp.fetch(url,options) Apps UrlFetchApp.fetch(url,options) Apps Apps.

API , . API Google Cloud. "" " " API- .

function updateContent(scriptId,content,theAccessTkn) {
//try{
  var options,payload,response,url;

  if (!content) {
    //Error handling function
    return;
  }

  if (!theAccessTkn) {
    theAccessTkn = ScriptApp.getOAuthToken();
  }

  //https://developers.google.com/apps-script/api/reference/rest/v1/projects/updateContent
  url = "https://script.googleapis.com/v1/projects/" + scriptId + "/content";

  options = {
    "method" : "PUT",
    "muteHttpExceptions": true,
    "headers": {
      'Authorization': 'Bearer ' +  theAccessTkn
     },
    "contentType": "application/json",//If the content type is set then you can stringify the payload
    "payload": JSON.stringify(content)
  };

  response = UrlFetchApp.fetch(url,options);
  response = JSON.parse(response);//Must be parsed even though it shows as coming back as an object

  //Logger.log('typeof response: ' + typeof response)

  //Logger.log('response 29 in file GS_Update: ' + JSON.stringify(response).slice(0,45))

  return response;
//} catch(e) {
  //Logger.log(response)
//}
};

, .

Scopes can be set in the appsscript.json file. To view the appsscript.json file, you must first click the View menu and select the Show manifest menu item.

{
  "timeZone": "America/New_York",
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.projects",
    "https://www.googleapis.com/auth/script.external_request"
  ],
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER"
}

The first time you use the application script API, the PUT request may not work and will return an error with a link to the Google Cloud console. This is why it is important to look at the response Logger.log('typeof response: ' + typeof response)from the response = UrlFetchApp.fetch(url,options);expression.

0
source

Source: https://habr.com/ru/post/1621808/


All Articles