Last active
May 10, 2019 18:43
-
-
Save grzegorzblaszczyk/8615267 to your computer and use it in GitHub Desktop.
Delete all but last worksheet in Google Docs Spreadsheet using Google Spreadsheet API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_of_sheets.xml | |
nodes.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "x$1" == "x" ]; then | |
echo "" | |
echo "Usage: $0 [email_address like [email protected]] [password] [worksheet_key]" | |
echo "" | |
exit 1; | |
fi | |
# 1. Credentials | |
EMAIL_ADDRESS="$1" | |
PASSWD="$2" | |
WORKSHEET_KEY="$3" | |
CURL=`which curl` | |
XPATH=`which xpath` | |
### DO NOT EDIT BELOW THIS LINE ### | |
# 2. Get Auth token | |
AUTH=`$CURL -X POST -d "accountType=GOOGLE&Email=${EMAIL_ADDRESS}&Passwd=${PASSWD}&service=wise" https://www.google.com/accounts/ClientLogin | grep Auth` | |
# 3. Get list of worksheets for spreadsheet | |
$CURL -v -H "Authorization: GoogleLogin $AUTH" -H "GData-Version: 3.0" -H "Content-Type: application/atom+xml;charset=UTF-8" "https://spreadsheets.google.com/feeds/worksheets/${WORKSHEET_KEY}/private/full" > list_of_sheets.xml | |
# 4. Fetch list of sheets using XPath | |
$XPATH list_of_sheets.xml "//feed/entry/id/text()" | sed -e s/"https"/" https"/g > nodes.txt # id of entries | |
# 5. Count all worksheets | |
ALL_SHEETS=`wc -w nodes.txt | awk '{print $1; }'` | |
# 6. Iterate over all but last one and delete them | |
counter=1 | |
for i in `cat nodes.txt`; do | |
if [ $counter -lt $ALL_SHEETS ]; then | |
worksheet_id=`echo $i | cut -f 7 -d "/"`; | |
echo "Deleting worksheet ${worksheet_id}"; | |
$CURL -v -H "Authorization: GoogleLogin Auth=$AUTH" -H "GData-Version: 3.0" -H "Content-Type: application/atom+xml;charset=UTF-8" -H "If-Match: *" --request DELETE "https://spreadsheets.google.com/feeds/worksheets/0AgAbNlgtXR_RdFFUVHgzeWZFTkl2TjVQS2xROVU5Rnc/private/full/${worksheet_id}" | |
fi | |
counter=$(($counter+1)); | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see the below Log when I execute the script :
Connected to spreadsheets.google.com (172.217.26.206) port 443 (#0)
CApath: none
} [229 bytes data]
{ [96 bytes data]
{ [3198 bytes data]
{ [115 bytes data]
{ [4 bytes data]
} [37 bytes data]
} [1 bytes data]
} [16 bytes data]
{ [1 bytes data]
{ [16 bytes data]
< HTTP/2 401
< content-type: text/html; charset=UTF-8
< x-robots-tag: noindex, nofollow, nosnippet
< www-authenticate: GoogleLogin realm="https://accounts.google.com//ClientLogin", service="wise"
< p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
< p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
< x-chromium-appcache-fallback-override: disallow-fallback
< date: Fri, 10 May 2019 18:41:07 GMT
< expires: Fri, 10 May 2019 18:41:07 GMT
< cache-control: private, max-age=0
< x-content-type-options: nosniff
< x-frame-options: SAMEORIGIN
< x-xss-protection: 1; mode=block
< server: GSE
< set-cookie: NID=183=wkkrTqdzlt4RbPyi6sMwiXtzecczbaqGB_qPGBeG4r5w2ZXpvkWCyKEL1ESCpNND0t6kIRFTwZQYD4PwKOfVYkG_fEQsFf3yT8ihmzp1ZwYD80dOXheuNSKFcCKlAkEZTdrch-ovVWLjDtgFocc2jPWY_1cqhf1uMaRe8QwFUUY;Domain=.google.com;Path=/;Expires=Sat, 09-Nov-2019 18:41:07 GMT;HttpOnly
< set-cookie: NID=183=Bagi7fRrosLpCkwbA346ELr3vBp7815O7YcwOKH2RGDjgP2roBkjdYxuKE2G6SqAXfDkJky2HDPW70a9YX6wQ0bK2UttCL889L5ZN4hM773wOOPPIpN9G3u2403XZuu0xkj5QtDovjmv9AUw9SjwbieUorf_zeoSsyUnUyc8dN4;Domain=.google.com;Path=/;Expires=Sat, 09-Nov-2019 18:41:07 GMT;HttpOnly
< alt-svc: quic=":443"; ma=2592000; v="46,44,43,39"
< accept-ranges: none
< vary: Accept-Encoding
<
{ [161 bytes data]
100 161 0 161 0 0 272 0 --:--:-- --:--:-- --:--:-- 273
No nodes found