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

前端 未结 3 987
不思量自难忘°
不思量自难忘° 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.

提交回复
热议问题