To demonstrate how Workiva Scripting works in Workiva, we've built a simple script that updates a cell in a spreadsheet. You can follow these steps to replicate the script in your own workspace.
Note: Scripting is currently only available through Customize Workiva. Learn more.
要求
- Workiva Scripting must be enabled for your workspace.Learn more about Workiva Scripting.
- The Script Editor role is required to create or edit scripts.
- You must generate dependencies in order to run this script directly from a spreadsheet. This will include the "requests" library necessary to interact with the Workiva Public APIs.
Build the script
To build the script in your workspace, follow these steps:
- From Home, click + Create => Script.
- On the right side, enter a name and brief description for the script.
- Go the the script editor in the middle of the screen and delete any existing code. Copy and paste in the following:
import requests import json import os AUTH_URL = "https://api.app.wdesk.com/iam/v1/oauth2/token" SS_API_URL = 'https://api.app.wdesk.com/platform/v1/spreadsheets/' CLIENT_ID = os.getenv('CLIENT_ID') CLIENT_SECRET = os.getenv('CLIENT_SECRET') SPREADSHEET_ID = os.getenv('SPREADSHEET_ID') SHEET_ID = os.getenv('SHEET_ID') HELLO_WORLD = os.getenv('HELLO_WORLD') class ApiAuth: def __init__(self): self._headers = {'Content-Type': \ 'application/x-www-form-urlencoded;charset=UTF-8'} def getAuthToken(self): tokenRes = requests.post(AUTH_URL, data = 'client_id=' + CLIENT_ID + \ '&client_secret=' + CLIENT_SECRET + '&grant_type=client_credentials', \ headers = self._headers) tokenResponse = json.loads(tokenRes.text) return tokenResponse['access_token'] class SSApi: def __init__(self, accessToken): self._accessToken = 'Bearer ' + accessToken self._ssApiUrl = SS_API_URL + SPREADSHEET_ID + "/sheets/" + SHEET_ID + \ "/values/A2:H" self._headers = {'Authorization': self._accessToken} def getRawData(self): dataRes = requests.get(self._ssApiUrl, headers = self._headers) # print(dataRes) rawData = json.loads(dataRes.text) # todo handle pagination # print("---", rawData['data'][0]['values'][0]) return rawData['data'][0]['values'] def updateRange(self, sheetId, range, values): url = SS_API_URL + SPREADSHEET_ID + "/sheets/" + sheetId + "/values/" + \ range print("Update Range: ", url) print("Values: ", values) dataRes = requests.put(url, headers = self._headers, \ data=json.dumps(values)) def write_tier_data(ssApi: SSApi): values = [] values.append([HELLO_WORLD]) ssApi.updateRange(SHEET_ID, 'A1:ZZZZ99', {"values": values}) def main(): authToken = ApiAuth().getAuthToken() ssApi = SSApi(authToken) # rawData = ssApi.getRawData() write_tier_data(ssApi) print('Calling main') main()
- 按一下「儲存」(Save)。
At this point, you should ensure you've generated the necessary dependencies for this script.
Run the script
To manually the script:
- With the script open, click Run Script in the menu bar at the top of the screen.
- Enter the parameters needed to access the Spreadsheets API, the spreadsheet and sheet IDs, and the text content for the cell. Click Run Script.
Note: The IDs can be found in the spreadsheet's URL: /spreadsheet/{spreadsheetId}/sheet/{sheetId}
.
To verify the script completes, check the updated cell in the spreadsheet.
Run the script directly from a spreadsheet
This script, and others like it, can also be run directly from a spreadsheet through the use of automations.
To use this script with automations:
- Delete the existing code.
- Replace it with this code:
import requests import json import os AUTH_URL = "https://api.sandbox.wdesk.com/iam/v1/oauth2/token" SS_API_URL = 'https://api.sandbox.wdesk.com/platform/v1/spreadsheets/' # CLIENT_ID = os.getenv('CLIENT_ID') # CLIENT_SECRET = os.getenv('CLIENT_SECRET') # SPREADSHEET_ID = os.getenv('SPREADSHEET_ID') # SHEET_ID = os.getenv('SHEET_ID') # HELLO_WORLD = os.getenv('HELLO_WORLD') CLIENT_ID = "" # Enter your client Id here CLIENT_SECRET = "" # Enter your client secret here DOCUMENT_ID = os.getenv('DOCUMENT_ID') INPUT_SHEET_ID = os.getenv('INPUT_SHEET_ID') INPUT_RESOURCE_ID = os.getenv('INPUT_RESOURCE_ID') def getSpreadsheetId(wurl): start = wurl.find("sheets_") if (start >= 0): return wurl[start + len("sheets_"):] return None def getSpreadsheetSectionId(sheetId, wurl): start = wurl.find("sheets_" + sheetId + "_") if (start >= 0): return wurl[start + len("sheets_" + sheetId + "_"):] return None SPREADSHEET_ID = getSpreadsheetId(INPUT_SHEET_ID) if SPREADSHEET_ID != None : SHEET_ID = getSpreadsheetSectionId(SPREADSHEET_ID, INPUT_RESOURCE_ID) HELLO_WORLD = "Holaaaaaaaaaa" class ApiAuth: def __init__(self): self._headers = {'Content-Type': \ 'application/x-www-form-urlencoded;charset=UTF-8'} def getAuthToken(self): tokenRes = requests.post(AUTH_URL, data = 'client_id=' + CLIENT_ID + \ '&client_secret=' + CLIENT_SECRET + '&grant_type=client_credentials', \ headers = self._headers) tokenResponse = json.loads(tokenRes.text) return tokenResponse['access_token'] class SSApi: def __init__(self, accessToken): self._accessToken = 'Bearer ' + accessToken self._ssApiUrl = SS_API_URL + SPREADSHEET_ID + "/sheets/" + SHEET_ID + \ "/values/A2:H" self._headers = {'Authorization': self._accessToken} def getRawData(self): dataRes = requests.get(self._ssApiUrl, headers = self._headers) # print(dataRes) rawData = json.loads(dataRes.text) # todo handle pagination # print("---", rawData['data'][0]['values'][0]) return rawData['data'][0]['values'] def updateRange(self, sheetId, range, values): url = SS_API_URL + SPREADSHEET_ID + "/sheets/" + sheetId + "/values/" + \ range print("Update Range: ", url) print("Values: ", values) dataRes = requests.put(url, headers = self._headers, \ data=json.dumps(values)) def write_tier_data(ssApi: SSApi): values = [] values.append([HELLO_WORLD]) ssApi.updateRange(SHEET_ID, 'A1:ZZZZ99', {"values": values}) def main(): authToken = ApiAuth().getAuthToken() ssApi = SSApi(authToken) # rawData = ssApi.getRawData() write_tier_data(ssApi) print('Calling main') main()
- Go to this article and follow the steps to run the script from your spreadsheet.