简体   繁体   中英

Can I automate the process of creating a new spreadsheet in Google Sheets from a CSV with a pre-written Apps Script to be added to the Script Editor?

I'm writing a Python program that takes in a handful of daily-downloaded CSV's, and the idea is to have each spreadsheet automatically, and separately uploaded to Google Sheets with new spreadsheets created from these CSV's. Also, I need each newly created spreadsheet to have an Apps Script uploaded to it. Everything I've seen thus far is how to perform actions on existing spreadsheets and not how to generate a new one from a file. And I haven't seen whether or not Google allows scripts to be programmatically added rather than manually added (last one being from several years ago).

Note: Combining the daily CSV's isn't possible since the client needs each spreadsheet to be sent to different recipients.

Goal

  • You download daily a certain amount of CSVs and want to convert them to Google Spreadsheets
  • You want to attach to each of the created spreadsheets an Apps Script file

Workflow

There are several possibilities to realize this functionality, in order to perform the process automatically on trigger, you can use Apps Script as described below:

Below is a sample function that creates a new Spreadsheet with an Apps Script file and automatically writes content into this file:

function createSpreadsheetwithScript() {

  var ss=SpreadsheetApp.create('myCSV');
  var id=ss.getId();
  var token = ScriptApp.getOAuthToken();
  var url = "https://script.googleapis.com/v1/projects";
  var payload = {
    "title": "myAutoCreatedScript",
    "parentId": id
  }
  var options = {
    "method" : "POST",
    "muteHttpExceptions": true,
    "headers": {
       'Authorization': 'Bearer ' +  token
     },
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  }
  var response = UrlFetchApp.fetch(url,options);
  var scriptId=JSON.parse(response).scriptId;
  var url2="https://script.googleapis.com/v1/projects/"+scriptId+"/content";
 //test content 
  var source="function myFunction() {\n var x=1;\n}";
  var JSONsource="{\"timeZone\":\"America/New_York\",\"exceptionLogging\":\"STACKDRIVER\"}";  
   var payload2 = {
  "files": [
    {
      "name": "this is the gs. file",
      "type": "SERVER_JS",
      "source": source
    },
    {
      "name": "appsscript",
      "type": "JSON",
      "source": JSONsource,
   "updateTime":"2018-03-04T19:49:08.871Z",
    "functionSet":{
      "values":[{"name":"myFunction"}]}
    }
  ]
}

var options2 = {
  "headers": {
     'Authorization': 'Bearer ' + token,
   }, 
  "contentType": "application/vnd.google-apps.script+json",
  "method" : "PUT", 
  "payload": JSON.stringify(payload2)
  }
  var response2 = UrlFetchApp.fetch(url2,options2); 
}

Make sure to update the contents of the appsscript.json file with the necessary authorization scopes:

{
  "timeZone": "America/New_York",
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Drive",
      "serviceId": "drive",
      "version": "v2"
    }]
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/script.projects", "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/script.scriptapp"]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM