Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinuasa/71161fddedce96246a1596f2f91efcd7 to your computer and use it in GitHub Desktop.
Save kinuasa/71161fddedce96246a1596f2f91efcd7 to your computer and use it in GitHub Desktop.
kintoneにアップロードした添付ファイルを一括でダウンロードするPower Automate for desktopフロー(動作確認:バージョン 2.40 )
/# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
APIトークンなどの変数設定
☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
SET APIToken TO $'''(APIトークン)'''
SET SubDomain TO $'''(サブドメイン)'''
Text.FromNumber Number: 6 DecimalPlaces: 0 UseThousandsSeparator: False FormattedNumber=> AppID
SET SaveFolderPath TO $'''C:\\Test\\kintone\\Files'''
/# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
添付ファイルフィールドのフィールドコードをリストに設定
☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
Variables.CreateNewList List=> AttachmentFieldCodeList
Variables.AddItemToList Item: $'''添付ファイル1''' List: AttachmentFieldCodeList
Variables.AddItemToList Item: $'''添付ファイル2''' List: AttachmentFieldCodeList
/# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
レコードの一括取得
☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
Web.InvokeWebService.InvokeWebService Url: $'''https://%SubDomain%.cybozu.com/k/v1/records.json?app=%AppID%''' Method: Web.Method.Get Accept: $'''application/json''' ContentType: $'''%''%''' CustomHeaders: $'''X-Cybozu-API-Token: %APIToken%''' ConnectionTimeout: 30 FollowRedirection: True ClearCookies: False FailOnErrorStatus: False EncodeRequestBody: False UserAgent: $'''Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.21) Gecko/20100312 Firefox/3.6''' Encoding: Web.Encoding.AutoDetect AcceptUntrustedCertificates: False ResponseHeaders=> WebServiceResponseHeaders Response=> WebServiceResponse StatusCode=> StatusCode
IF StatusCode <> 200 THEN
EXIT Code: 0 ErrorMessage: $'''レコードの取得に失敗しました。'''
END
Variables.ConvertJsonToCustomObject Json: WebServiceResponse CustomObject=> JsonAsCustomObject
/# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
添付ファイル名とファイルキーをデータテーブルに格納
☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
SET DataTable TO { ^['FileName', 'FileKey'] }
LOOP FOREACH Record IN JsonAsCustomObject['records']
LOOP FOREACH AttachmentFieldCode IN AttachmentFieldCodeList
LOOP FOREACH Attachment IN Record[AttachmentFieldCode]['value']
/# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
ファイル名として使用できない文字を置換
☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
Text.Replace Text: Attachment['name'] TextToFind: $'''[<>:\\\"\\/\\\\\\|\\?\\*]''' IsRegEx: True IgnoreCase: False ReplaceWith: $'''%''%''' ActivateEscapeSequences: False Result=> AttachmentName
Variables.AddRowToDataTable.AppendRowToDataTable DataTable: DataTable RowToAdd: [AttachmentName, Attachment['fileKey']]
END
END
END
/# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
データテーブルに格納されたファイル名とファイルキーを使ってファイルをダウンロード
☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
LOOP FOREACH CurrentItem IN DataTable
SET SaveFilePath TO $'''%SaveFolderPath%\\%CurrentItem['FileName']%'''
/# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
保存先にすでに同名のファイルが存在している場合に別名として保存
☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
IF (File.IfFile.Exists File: SaveFilePath) THEN
File.GetPathPart File: SaveFilePath RootPath=> RootPath Directory=> Directory FileName=> FileName FileNameWithoutExtension=> FileNameNoExtension Extension=> FileExtension
LOOP LoopIndex FROM 1 TO 255 STEP 1
SET SaveFilePath TO $'''%SaveFolderPath%\\%FileNameNoExtension%(%LoopIndex%)%FileExtension%'''
IF (File.IfFile.DoesNotExist File: SaveFilePath) THEN
EXIT LOOP
END
END
END
Web.InvokeWebService.InvokeWebServiceSaveToFile Url: $'''https://%SubDomain%.cybozu.com/k/v1/file.json?fileKey=%CurrentItem['FileKey']%''' Method: Web.Method.Get Accept: $'''%''%''' ContentType: $'''%''%''' CustomHeaders: $'''X-Cybozu-API-Token: %APIToken%''' FilePath: SaveFilePath ConnectionTimeout: 30 FollowRedirection: True ClearCookies: False FailOnErrorStatus: False EncodeRequestBody: False UserAgent: $'''Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.21) Gecko/20100312 Firefox/3.6''' Encoding: Web.Encoding.AutoDetect AcceptUntrustedCertificates: False ResponseHeaders=> WebServiceResponseHeaders DownloadedFile=> DownloadedFile StatusCode=> StatusCode
IF StatusCode <> 200 THEN
EXIT Code: 0 ErrorMessage: $'''%CurrentItem['FileName']%ファイルのダウンロードに失敗しました。'''
END
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment