简体   繁体   English

你可以在Python上使用Google Apps脚本吗?

[英]Can you use Google Apps Script with Python?

Google Apps Script looks to be pretty perfect for a school project, however I'm not terribly comfortable with JavaScript and the entire rest of the project is going to be done in Python. 对于学校项目来说, Google Apps脚本看起来非常完美,但是我对JavaScript并不十分满意,而整个项目的其余部分都是用Python完成的。 Is there a way to access it using a Python library? 有没有办法使用Python库访问它? Or do I need to suck it up and learn JavaScript? 或者我需要吸收它并学习JavaScript吗?

This tutorial is the closest thing I've found in my searching and isn't quite what I want. 本教程是我在搜索中发现的最接近的东西,并不是我想要的。

No, Google Apps Script is its own programming language. 不,Google Apps脚本是它自己的编程语言。 There are a number of APIs for individual Google Apps , but they are not as comprehensive as what is provided via Google Apps Script. 各个Google Apps都有许多API ,但它们并不像通过Google Apps脚本提供的那样全面。 They're generally focused on providing access to the data, and might be suitable if you don't need to edit it. 它们通常专注于提供对数据的访问,如果您不需要编辑它,它们可能是合适的。

It is now possible to make requests to Google Apps Script from Python via the new Execution API , which uses a REST interface. 现在可以通过使用REST界面的新Execution API从Python向Google Apps脚本发出请求。 Related blog post announcement. 相关博文发布公告。 Learning some JS is still required. 学习一些JS仍然是必需的。

Never tried this but maybe you can use JSON (javascript object notation) 从未尝试过这个,但也许你可以使用JSON(javascript对象表示法)

In the Utilities class of Google apps script you can find the some methods (classes) that refer to this notation. 在Google Apps应用程序脚本的Utilities类中,您可以找到引用此表示法的一些方法(类)。 jsonParse and jsonStringify https://developers.google.com/apps-script/class_utilities jsonParsejsonStringify https://developers.google.com/apps-script/class_utilities

And on the other hand python has a json encoder and decoder to do the same thing on that side. 而另一方面,python有一个json编码器解码器在那边做同样的事情。 http://docs.python.org/library/json.html http://docs.python.org/library/json.html

There are also two tutorials which refer to these json methods for Google Apps Script developers.google.com apps-script articles picasa_google_apis (being a newby I am not allowed to put full links) 还有两个教程可以参考Google Apps脚本开发人员的这些json方法.google.com应用程序 - 脚本文章picasa_google_apis(作为newby我不允许放置完整链接)

And this as a background link 这是一个背景链接
json.org json.org

Than the last thing to avoid great frustrations... check out if one of these issues might be on your way to heaven. 比最后一件事要避免很大的挫折......看看这些问题中的一个是否可能正在通往天堂。

  • Issue 1397 问题1397
  • Issue 470 问题470
  • Issue 220 问题220
  • Issue 445 问题445

Python is amazing, and one of its most amazing qualities is being able to serve as a "glue" of sorts between different modules of a system (regardless of language). Python是惊人的,其最令人惊叹的品质之一是能够作为系统的不同模块之间的各种“粘合剂”(不论语言)。

My suggestion is to try and make an Adapter/Wrapper around the Javascript commands you will need from Google App Script, exposing pure python functions to the rest of your program so it makes it easier on you. 我的建议是尝试使用Google App Script中需要的Javascript命令创建一个Adapter / Wrapper,将纯Python函数暴露给你的程序的其余部分,这样就可以让你更轻松。 In the end, you will still require to learn some Javascript so... get going. 最后,你仍然需要学习一些Javascript,所以...继续。

Google Apps Scripts uses Javascript only to manipulate classes of Google products and some more general classes for doing things outside Google. Google Apps Scripts仅使用Javascript来操纵Google产品类以及一些更常用的类,以便在Google之外执行操作。 Javascript is the only language. Javascript是唯一的语言。

So I came across this post because I also wanted to do the same thing for calculating moist air properties, which there are many free calculation tools out there, one of which is a python package. 所以我遇到了这个帖子,因为我也想做同样的事情来计算潮湿的空气属性,那里有许多免费的计算工具,其中一个是python包。 2 days later... I'm not sure if how I interpreted this post is a complete match to my problem and solution, so I will describe briefly what I did. 2天后...我不确定我是如何解释这篇文章是完全符合我的问题和解决方案,所以我将简要描述我做了什么。 I've documented the detailed steps in detail in this post 我已经记录了对此进行了详细的详细步骤

My goal was to have general access to python packages through Google Sheets via GAS, similar to how you can build custom Excel VBA macros that have extensions to many 3rd party libraries and COM interfaces. 我的目标是通过GAS通过Google表格一般访问python包,类似于如何构建具有许多第三方库和COM接口扩展的自定义Excel VBA宏。 The way I implemented it was to first publish a simple WebApp fruitfarmapp on the cloud - also using Google's free trial GAE, and then make a request to this WebApp using the GAS function UrlFetchApp() and then unpack the JSON using GAS. 我实现它的方法是首先在云上发布一个简单的WebApp fruitfarmapp--也使用Google的免费试用GAE,然后使用GAS函数UrlFetchApp()向此WebApp发出请求,然后使用GAS解压缩JSON。

A few comments 一些评论

  1. Performance tip - Minimize API communication - use batch requests The way that I have it implemented now is only proof-of-concept, so I'm returning each value one-at-a-time. 性能提示 - 最小化API通信 - 使用批处理请求我现在实现它的方式只是概念验证,所以我一次一个地返回每个值。 This wouldn't be how you wouldd do it if you need to process array of data -- for example if you have a database of sensor values of temperature and humidity. 如果您需要处理数据数组,那么就不会这样做 - 例如,如果您有温度和湿度传感器值的数据库。 In this case you should process the response as a batch request, because the API communication is the bottleneck slow step, so you want to minimize the API fetch and do as much as you can either in the GAS or in your WebApp. 在这种情况下,您应该将响应作为批处理请求进行处理,因为API通信是瓶颈慢的步骤,因此您希望最小化API提取并尽可能多地在GAS或WebApp中执行。

  2. General comment - this was really tedious . 一般评论 - 这真的很乏味 I would strongly recommend to try another method if at all possible. 如果可能的话,我强烈建议尝试另一种方法。 Building and deploying a WebApp just to do a simple calc is not a good use of time, especially if you are a novice like me. 构建和部署WebApp只是为了做一个简单的计算并不能很好地利用时间,特别是如果你像我这样的新手。 This took a total of 20hrs to build the WebApp from scratch, deploy it, debug various nuances, etc. The easiest part was the GAS script ~ 1 hr once the API was deployed. 这花费了20个小时从头开始构建WebApp,部署它,调试各种细微差别等。最简单的部分是GAS脚本〜部署API后1小时。

Not sure if this is helpful or not, Good luck! 不确定这是否有用,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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