如何从Python脚本向Google App Script API传递参数?

如何从Python脚本向Google App Script API传递参数?,第1张

概述我有一个想传递给Google App脚本参数SalesID.如何使用此python脚本传递它?(脚本来自https://developers.google.com/apps-script/api/how-tos/execute)from __future__ import print_function from googleapiclient import

我有一个想传递给Google App脚本的参数SalesID.如何使用此python脚本传递它?

(脚本来自https://developers.google.com/apps-script/api/how-tos/execute)

from __future__ import print_functionfrom Googleapiclient import errorsfrom Googleapiclient.discovery import buildfrom httplib2 import httpfrom oauth2client import file as oauth_file,clIEnt,toolsdef main(SalesID):    """Runs the sample.    """    SCRIPT_ID = '1WChnVrk5gycQEtumI7mPi5PexXafuhBAWN7-VnBK2aPkFpzMHtUp0cnx' #Actual Google Sheet Sample    # Setup the Apps Script API    ScopES = ['https://www.GoogleAPIs.com/auth/script.projects','https://www.GoogleAPIs.com/auth/spreadsheets']    store = oauth_file.Storage('token.Json')    creds = store.get()    if not creds or creds.invalID:        flow = clIEnt.flow_from_clIEntsecrets('credentials.Json',ScopES)        creds = tools.run_flow(flow,store)    service = build('script','v1',http=creds.authorize(http()))    # Create an execution request object.    request = {"function": "getFoldersUnderRoot"}    try:        # Make the API request.        response = service.scripts().run(body=request,scriptID=SCRIPT_ID).execute()        if 'error' in response:            # The API executed,but the script returned an error.            # Extract the first (and only) set of error details. The values of            # this object are the script's 'errorMessage' and 'errorType',and            # an List of stack trace elements.            error = response['error']['details'][0]            print("Script error message: {0}".format(error['errorMessage']))            if 'scriptStackTraceElements' in error:                # There may not be a stacktrace if the script dIDn't start                # executing.                print("Script error stacktrace:")                for trace in error['scriptStackTraceElements']:                    print("\t{0}: {1}".format(trace['function'],trace['lineNumber']))        else:            # The structure of the result depends upon what the Apps Script            # function returns. Here,the function returns an Apps Script Object            # with String keys and values,and so the result is treated as a            # Python dictionary (folderSet).            folderSet = response['response'].get('result',{})            if not folderSet:                print('No folders returned!')            else:                print('Folders under your root folder:')                for (folderID,folder) in folderSet.iteritems():                    print("\t{0} ({1})".format(folder,folderID))    except errors.httpError as e:        # The API encountered a problem before the script started executing.        print(e.content)if __name__ == '__main__':    main()

Google表格样本:https://docs.google.com/spreadsheets/d/1Z4PAY3CCaRorn5LRdFQKn4-EcAHxwxHJsABzEgsSQk0/edit#gid=0

Google应用脚本的功能:

function myFunction(e) {var ss = SpreadsheetApp.getActiveSpreadsheet();var sheet = ss.getSheetByname("Sheet1");var timestamp = new Date();sheet.getRange("A2").setValue(e.parameter.SalesID);sheet.getRange("B2").setValue(timestamp);}

基本上,我希望能够将SalesID从外壳传递到Google应用脚本进行处理.谢谢!最佳答案如official documentation中所述,您必须在请求正文中提供它:

request = {"function": "myFunction","parameters": [{"salesID" : 123}]}

也使用setValue(e.salesID); 总结

以上是内存溢出为你收集整理的如何从Python脚本向Google App Script API传递参数? 全部内容,希望文章能够帮你解决如何从Python脚本向Google App Script API传递参数? 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199551.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存