Update (Overwrite) one Apps Script file with another Apps Script file using Apps Script Code

前端 未结 3 961
不思量自难忘°
不思量自难忘° 2021-01-03 06:45

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

相关标签:
3条回答
  • 2021-01-03 07:27

    The new Apps Script API now allows for an Apps Script project to be updated. The project that is updated can be bound to a document. (Sheet, Form, Doc) This code uses the REST API, and makes a PUT request from Apps Script code using UrlFetchApp.fetch(url,options) This code is being run from an Apps Script file to update another Apps Script file.

    The Apps Script API must be enabled for the Apps Script file running the code. The Apps Script API is enabled in the Google Cloud console. From the code editor, choose "Resources" and "Cloud Platform project" Search for Apps Script API and enable it.

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

    You must use the correct scopes in order for the code to run without an authorization error.

    The scopes can be set in the appsscript.json file. To view the appsscript.json file, you must first click the View menu, and choose 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 that the Apps Script API is used, the PUT request may not work, and will return an error with a link to the Google Cloud console. That's why it's important to view the return response Logger.log('typeof response: ' + typeof response) from the response = UrlFetchApp.fetch(url,options); statement.

    0 讨论(0)
  • 2021-01-03 07:28

    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)
    }
    
    0 讨论(0)
  • 2021-01-03 07:40

    I've created a GitHub repository of a project that will update one Apps Script file (the target) from a source Apps Script file. It's totally free to copy and use.

    GitHub Repository - apps-script-update

    See the Read Me file in the GitHub repository for instructions

    The code at GitHub is using the newer Apps Script API, which is different from the original answer.

    0 讨论(0)
提交回复
热议问题