Skip to content

Instantly share code, notes, and snippets.

@grzegorzblaszczyk
Last active May 10, 2019 18:43
Show Gist options
  • Save grzegorzblaszczyk/8615267 to your computer and use it in GitHub Desktop.
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
list_of_sheets.xml
nodes.txt
#!/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
@varunpandurangi
Copy link

Can you please post a sample command how to post this on terminal ? I tried supplying email id, password and spreadsheet id as parameters , but no success.

@varunpandurangi
Copy link

I see the below Log when I execute the script :

Connected to spreadsheets.google.com (172.217.26.206) port 443 (#0)

  • ALPN, offering h2
  • ALPN, offering http/1.1
  • Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@strength
  • successfully set certificate verify locations:
  • CAfile: /etc/ssl/cert.pem
    CApath: none
  • TLSv1.2 (OUT), TLS handshake, Client hello (1):
    } [229 bytes data]
  • TLSv1.2 (IN), TLS handshake, Server hello (2):
    { [96 bytes data]
  • TLSv1.2 (IN), TLS handshake, Certificate (11):
    { [3198 bytes data]
  • TLSv1.2 (IN), TLS handshake, Server key exchange (12):
    { [115 bytes data]
  • TLSv1.2 (IN), TLS handshake, Server finished (14):
    { [4 bytes data]
  • TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
    } [37 bytes data]
  • TLSv1.2 (OUT), TLS change cipher, Client hello (1):
    } [1 bytes data]
  • TLSv1.2 (OUT), TLS handshake, Finished (20):
    } [16 bytes data]
  • TLSv1.2 (IN), TLS change cipher, Client hello (1):
    { [1 bytes data]
  • TLSv1.2 (IN), TLS handshake, Finished (20):
    { [16 bytes data]
  • SSL connection using TLSv1.2 / ECDHE-ECDSA-CHACHA20-POLY1305
  • ALPN, server accepted to use h2
  • Server certificate:
  • subject: C=US; ST=California; L=Mountain View; O=Google LLC; CN=*.google.com
  • start date: Apr 16 09:57:40 2019 GMT
  • expire date: Jul 9 09:52:00 2019 GMT
  • subjectAltName: host "spreadsheets.google.com" matched cert's "*.google.com"
  • issuer: C=US; O=Google Trust Services; CN=Google Internet Authority G3
  • SSL certificate verify ok.
  • Using HTTP2, server supports multi-use
  • Connection state changed (HTTP/2 confirmed)
  • Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
  • Using Stream ID: 1 (easy handle 0x7fc573807200)

GET /feeds/worksheets/177TxmzgN4h-vQj5byqBFpcGlGwNW0whtmlq8WyW3Usc/private/full HTTP/2
Host: spreadsheets.google.com
User-Agent: curl/7.54.0
Accept: /
Authorization: GoogleLogin https://developers.google.com/accounts/docs/AuthForInstalledApps
GData-Version: 3.0
Content-Type: application/atom+xml;charset=UTF-8

  • Connection state changed (MAX_CONCURRENT_STREAMS updated)!
    < 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
  • Connection #0 to host spreadsheets.google.com left intact
    No nodes found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment