Cloud Variables 2021-03-23

Store/Read variables from the cloud with the help of a Google Spreadsheet with AutoSheets

  1. joaomgcd

    This is an example on how to use AutoSheets for Tasker variables that are stored in the cloud.
    This means that no matter which device you're on, you can get a consistent value for a certain variable.

    Import the project above and run the Test Cloud Variables task to see how it works. :cool:

    Breakdown of the actions in the Update Cloud Variable Task
    1. Multiple Variable Set is used to simply save some default values for %par1 and %par2 for testing. Not needed in a real situation and can be deleted if not needed. %par1 will contain the variable name you want to store in the cloud and %par2 will contain the variable value.
    2. If to check if no error is yet set. An error can be set if the actions below generate an error and a second attempt of writing the variable is done
    3. Variable Set %secondtime to 1 to mark that this is the second try. Below, if we get another error again, we'll stop the task instead of trying again if it's the second time we're trying.
    4. Variable Clear %errmsg so that we can try again fresh without the existing error.
    5. End If to close the If above
    6. AutoSheets Get Spreadsheet: Gets the ID for the spreadsheet named Cloud Variables (will error out if the spreadsheet doesn't exist yet)
    7. If no error in previous action (this means that the spreadsheet already exists) then get the existing variable names from the sheet by getting all the fields in the first column with AutoSheets Get Data and setting Output Array Names to varnames. This will create a %varnames() array in Tasker with all the contents of the first column of the sheet
    8. Flash (disabled): show the existing varnames and other info if needed. Disabled so it doesn't display normally, but you can enable it for testing
    9. If to check if the variable name you want to write already exists on the spreadsheet. If it does than the %varnames(#?%par1) expression should return a number different than 0. More info here.
    10. If the variable doesn't exist yet lets add it at the end of the variable list with the AutoSheets Add Rows action
    11. Else...
    12. Let's update the value for the existing variable using the AutoSheets Update Cells action
    13. End If above
    14. If the error we got in A6 while getting the Spreadheet ID includes the words no spreadsheet this means that the sheet doesn't exist yet. We need to create the sheet and then try again
    15. Stop the task if we already tried before and still got the same error... This shouldn't happen...
    16. Use AutoSheets Create Spreadsheet to create a new spreadsheet with the name Cloud Variables
    17. Goto the A2 action again and try it again all over again
    18. End If above