文字列で与えられたGoogle SpreadSheetsのURLから正規表現の機能を用いてSheets IDを抽出するAppleScriptです。
初回掲載時の内容にShane Stanleyから「長さが0の文字列」(zero length string)に対応できていないので、変更したほうがいいよ、という助言をもらったので書き換えました(Thanks Shane!)。
AppleScript名:Google Sheets URLから正規表現でIDを抽出 v2 |
— Created 2019-04-18 by Takaaki Naganoya — Modified 2019-04-19 by Shane Stanley use AppleScript version "2.5" –macOS 10.11 or later use scripting additions use framework "Foundation" property NSString : a reference to current application’s NSString property NSRegularExpressionSearch : a reference to current application’s NSRegularExpressionSearch –https://developers.google.com/sheets/guides/concepts?hl=ja set aURLText to "https://docs.google.com/spreadsheets/d/1qpyC0XzvTcKT6EISywvqESX3A0MwQoFDE8p-Bll4hps/edit#gid=0 " set sheetsID to (stripGoogleSheetsIDFromURL(aURLText) of me) as string –> "1qpyC0XzvTcKT6EISywvqESX3A0MwQoFDE8p-Bll4hps" set aURLText to "" –Zero Length String set sheetsID to (stripGoogleSheetsIDFromURL(aURLText) of me) as string –> "" on stripGoogleSheetsIDFromURL(aText as string) set sStrHead to "/spreadsheets/d/" set regStr to sStrHead & "([a-zA-Z0-9-_]+)" set anNSString to NSString’s stringWithString:aText set aRange to anNSString’s rangeOfString:regStr options:(NSRegularExpressionSearch) –if aRange = {location:0, length:0} then return ""–v1 if |length| of aRange = 0 then return "" –Prepare for zero length strings(Thanks Shane!) set bStr to anNSString’s substringWithRange:aRange set theString to bStr’s stringByReplacingOccurrencesOfString:sStrHead withString:"" options:(NSRegularExpressionSearch) range:{location:0, |length|:length of sStrHead} return theString as string end stripGoogleSheetsIDFromURL |
More from my site
(Visited 112 times, 1 visits today)