{"_id":"59ceba1c86221200280ec2c4","project":"5746675a5aa03c0e00fce9bc","version":{"_id":"5746675a5aa03c0e00fce9bf","__v":8,"project":"5746675a5aa03c0e00fce9bc","createdAt":"2016-05-26T03:02:50.849Z","releaseDate":"2016-05-26T03:02:50.849Z","categories":["5746675a5aa03c0e00fce9c0","574899e7e8c6a420000b7314","57489c11d929e919006ed4ae","57489c82d929e919006ed4b1","57489ce91b345d0e004e5231","5748a82e1b345d0e004e5256","59ce5c88e8c105001a4bedd7","59f96cd129b4d600246c2899"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"category":{"_id":"59ce5c88e8c105001a4bedd7","project":"5746675a5aa03c0e00fce9bc","version":"5746675a5aa03c0e00fce9bf","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-09-29T14:45:28.177Z","from_sync":false,"order":1,"slug":"blockspring-scripts","title":"Blockspring Scripts"},"user":"545988354f86a50800f9cce4","__v":0,"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2017-09-29T21:24:44.667Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":7,"body":"Let's learn how to use app connectors, and buttons to initiate scripts.\n[block:api-header]\n{\n  \"title\": \"Usecase: Click to Sync with Google Sheets\"\n}\n[/block]\nSpreadsheets are great for backing up information, as well as updating information en masse. In this tutorial, we'll write a script that exports Trello cards to Google Sheets, then lets us edit a bunch of cards at once from a sheet, and import back into Trello.\n\n## ENABLE A CONNECTOR\nUsually, integrating with another app requires setting up OAuth or getting an API token. Blockspring Scripts handles this so you can choose your connector, sign in, and get started with your integration.\n\n1) Click **Add+** next to **Connectors** in the right sidebar.\n2) Choose your connector, in this case **Google Sheets** .\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/f4494b4-connector.gif\",\n        \"connector.gif\",\n        794,\n        601,\n        \"#1178b0\"\n      ],\n      \"border\": true\n    }\n  ]\n}\n[/block]\nYou'll notice the editor automatically handles OAuth/authentication with Google, and imports `google_sheets_app`. You're now able to script with Sheets, just like you do with Trello.\n\n## EXPORT CARDS TO SHEET\nEnter in the following code to export all your open cards to Google Sheets.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import google_sheets_app\\nimport trello_app\\n\\n## STEP 1: SIGN IN WITH GOOGLE SHEETS IN THE SIDEBAR\\n## STEP 2: PASTE A GOOGLE SHEET'S ID BELOW (FOUND IN ITS URL)\\nsheet_id = \\\"PASTE_YOUR_SHEET_ID_HERE\\\"\\n\\ndef export_to_sheets():\\n    # get all open cards in this board\\n    board = trello_app.get_current_board()\\n    cards = board.get_cards(filter=\\\"open\\\")\\n\\n    # export cards to the google sheet\\n    sheet = google_sheets_app.get_spreadsheet_by_id(sheet_id).get_sheets()[0]\\n    sheet.write_values(cards)\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nSince we've already signed in with Google Sheets, the next step is to find a sheet ID. You'll want to do the following:\n\n1) Open a new or existing Google Sheet.\n2) Save the sheet and make sure it has a name.\n3) Copy the sheet ID, found in its URL, into your script.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/a0ec9fc-getsheetid.gif\",\n        \"getsheetid.gif\",\n        923,\n        688,\n        \"#e3e3e4\"\n      ],\n      \"border\": true\n    }\n  ]\n}\n[/block]\nFinally, let's run our export function. However, instead of using the test button in the editor, let's go back to Trello.\n\n1) Hover over the Blockspring leaf on your Trello board, \n2) Click the `export_to_sheets` menu option.\n\nCheck out the sheet - it has our cards!\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/ee6fcf0-runbutton.gif\",\n        \"runbutton.gif\",\n        865,\n        676,\n        \"#50b66d\"\n      ],\n      \"border\": true\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Running scripts from the Blockspring leaf\",\n  \"body\": \"Functions you add to your script are all available to run from the Blockspring leaf, outside of the script editor.\\n\\nGo to the board (or Asana project, FIeldbook book, etc) associated with your script, and instead of clicking the leaf to open the editor, just hover over the leaf and click the function you want to run it.\"\n}\n[/block]\n## IMPORT CARDS FROM SHEET\nAdding the ability to edit your cards in the Google Sheet and then importing is as easy as updating your function with the following code.\n\n*Don't forget to paste in your Sheet ID here as well.*\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import google_sheets_app\\nimport trello_app\\n\\n## STEP 1: SIGN IN WITH GOOGLE SHEETS IN THE SIDEBAR\\n## STEP 2: PASTE A GOOGLE SHEET'S ID BELOW (FOUND IN ITS URL)\\nsheet_id = \\\"PASTE_YOUR_SHEET_ID_HERE\\\"\\n\\ndef export_to_sheets():\\n    # get all open cards in this board\\n    board = trello_app.get_current_board()\\n    cards = board.get_cards(filter=\\\"open\\\")\\n\\n    # export cards to the google sheet\\n    sheet = google_sheets_app.get_spreadsheet_by_id(sheet_id).get_sheets()[0]\\n    sheet.write_values(cards)\\n\\ndef import_from_sheets():\\n    # read the google sheet\\n    sheet = google_sheets_app.get_spreadsheet_by_id(sheet_id).get_sheets()[0]\\n    sheet_data = sheet.get_values()\\n\\n    # for each row in sheet\\n    for row in sheet_data:\\n        # look for an ID column, and lookup that card\\n        card = trello_app.get_card_by_id(row.get(\\\"id\\\"))\\n\\n        # if you find a card, update it with the row\\n        if card:\\n            card.set_field(row)\\n        # if this card doesn't exist, create it\\n        else:\\n            li = trello_app.get_list_by_id(row.get(\\\"idList\\\"))\\n            li.create_card(row)\\n\\n    # when done, pull updated card list into sheets\\n    export_to_sheets()\\n\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nLet's walk through how this code works:\n\n- First, we get values from our spreadsheet's first sheet.\n- Next, we go through each row attempting to look up the card in Trello give the \"id\" column.\n- If a card exists, we use `.set_field()` to update it with whatever updated information we have in our spreadsheet.\n- If a card does not exist, we create it in using the \"idList\" column.\n- Finally, once all cards are updated or created, we run `export_to_sheets` to update our spreadsheet with the latest card list from Trello.\n\nHere's a GIF of what this look like when run from the Blockspring leaf on Trello.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/035d491-import.gif\",\n        \"import.gif\",\n        868,\n        672,\n        \"#50b66d\"\n      ],\n      \"border\": true\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Need Help?\"\n}\n[/block]\nIf you run into any issues, or what to chat with others, [connect to the Blockspring community](https://blockspring-help.readme.io/docs/getting-help) right from the Scripts editor.","excerpt":"","slug":"tutorial-app-connectors","type":"basic","title":"Tutorial: App Connectors and Buttons"}

Tutorial: App Connectors and Buttons


Let's learn how to use app connectors, and buttons to initiate scripts. [block:api-header] { "title": "Usecase: Click to Sync with Google Sheets" } [/block] Spreadsheets are great for backing up information, as well as updating information en masse. In this tutorial, we'll write a script that exports Trello cards to Google Sheets, then lets us edit a bunch of cards at once from a sheet, and import back into Trello. ## ENABLE A CONNECTOR Usually, integrating with another app requires setting up OAuth or getting an API token. Blockspring Scripts handles this so you can choose your connector, sign in, and get started with your integration. 1) Click **Add+** next to **Connectors** in the right sidebar. 2) Choose your connector, in this case **Google Sheets** . [block:image] { "images": [ { "image": [ "https://files.readme.io/f4494b4-connector.gif", "connector.gif", 794, 601, "#1178b0" ], "border": true } ] } [/block] You'll notice the editor automatically handles OAuth/authentication with Google, and imports `google_sheets_app`. You're now able to script with Sheets, just like you do with Trello. ## EXPORT CARDS TO SHEET Enter in the following code to export all your open cards to Google Sheets. [block:code] { "codes": [ { "code": "import google_sheets_app\nimport trello_app\n\n## STEP 1: SIGN IN WITH GOOGLE SHEETS IN THE SIDEBAR\n## STEP 2: PASTE A GOOGLE SHEET'S ID BELOW (FOUND IN ITS URL)\nsheet_id = \"PASTE_YOUR_SHEET_ID_HERE\"\n\ndef export_to_sheets():\n # get all open cards in this board\n board = trello_app.get_current_board()\n cards = board.get_cards(filter=\"open\")\n\n # export cards to the google sheet\n sheet = google_sheets_app.get_spreadsheet_by_id(sheet_id).get_sheets()[0]\n sheet.write_values(cards)", "language": "python" } ] } [/block] Since we've already signed in with Google Sheets, the next step is to find a sheet ID. You'll want to do the following: 1) Open a new or existing Google Sheet. 2) Save the sheet and make sure it has a name. 3) Copy the sheet ID, found in its URL, into your script. [block:image] { "images": [ { "image": [ "https://files.readme.io/a0ec9fc-getsheetid.gif", "getsheetid.gif", 923, 688, "#e3e3e4" ], "border": true } ] } [/block] Finally, let's run our export function. However, instead of using the test button in the editor, let's go back to Trello. 1) Hover over the Blockspring leaf on your Trello board, 2) Click the `export_to_sheets` menu option. Check out the sheet - it has our cards! [block:image] { "images": [ { "image": [ "https://files.readme.io/ee6fcf0-runbutton.gif", "runbutton.gif", 865, 676, "#50b66d" ], "border": true } ] } [/block] [block:callout] { "type": "info", "title": "Running scripts from the Blockspring leaf", "body": "Functions you add to your script are all available to run from the Blockspring leaf, outside of the script editor.\n\nGo to the board (or Asana project, FIeldbook book, etc) associated with your script, and instead of clicking the leaf to open the editor, just hover over the leaf and click the function you want to run it." } [/block] ## IMPORT CARDS FROM SHEET Adding the ability to edit your cards in the Google Sheet and then importing is as easy as updating your function with the following code. *Don't forget to paste in your Sheet ID here as well.* [block:code] { "codes": [ { "code": "import google_sheets_app\nimport trello_app\n\n## STEP 1: SIGN IN WITH GOOGLE SHEETS IN THE SIDEBAR\n## STEP 2: PASTE A GOOGLE SHEET'S ID BELOW (FOUND IN ITS URL)\nsheet_id = \"PASTE_YOUR_SHEET_ID_HERE\"\n\ndef export_to_sheets():\n # get all open cards in this board\n board = trello_app.get_current_board()\n cards = board.get_cards(filter=\"open\")\n\n # export cards to the google sheet\n sheet = google_sheets_app.get_spreadsheet_by_id(sheet_id).get_sheets()[0]\n sheet.write_values(cards)\n\ndef import_from_sheets():\n # read the google sheet\n sheet = google_sheets_app.get_spreadsheet_by_id(sheet_id).get_sheets()[0]\n sheet_data = sheet.get_values()\n\n # for each row in sheet\n for row in sheet_data:\n # look for an ID column, and lookup that card\n card = trello_app.get_card_by_id(row.get(\"id\"))\n\n # if you find a card, update it with the row\n if card:\n card.set_field(row)\n # if this card doesn't exist, create it\n else:\n li = trello_app.get_list_by_id(row.get(\"idList\"))\n li.create_card(row)\n\n # when done, pull updated card list into sheets\n export_to_sheets()\n", "language": "python" } ] } [/block] Let's walk through how this code works: - First, we get values from our spreadsheet's first sheet. - Next, we go through each row attempting to look up the card in Trello give the "id" column. - If a card exists, we use `.set_field()` to update it with whatever updated information we have in our spreadsheet. - If a card does not exist, we create it in using the "idList" column. - Finally, once all cards are updated or created, we run `export_to_sheets` to update our spreadsheet with the latest card list from Trello. Here's a GIF of what this look like when run from the Blockspring leaf on Trello. [block:image] { "images": [ { "image": [ "https://files.readme.io/035d491-import.gif", "import.gif", 868, 672, "#50b66d" ], "border": true } ] } [/block] [block:api-header] { "title": "Need Help?" } [/block] If you run into any issues, or what to chat with others, [connect to the Blockspring community](https://blockspring-help.readme.io/docs/getting-help) right from the Scripts editor.