- 投稿日:2020-09-13T23:40:04+09:00
ブランチ名を省略したgit pull が通らない時の対処
ブランチ名を省略した git pull コマンドで以下のエラーに遭遇するときがある。
$ git pull There is no tracking information for the current branch. Please specify which branch you want to rebase against. See git-pull(1) for details. git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/<branch> master即刻pullしたいのであれば、git pull origin masterと打ち直すのが解決策としては早い。
が、そもそもの原因としてカレントブランチのupstreamが解決されていないことを解決してやれば、
ブランチ名を省略してシンプルに git pull とコマンドを打つだけで済むようになる。なんでも省略できるものはした方が作業効率がよいので対処する。
ログ後半の推奨コマンドに従えばよく、例えばmasterブランチにいるのならば以下コマンドを実行する。$ git branch --set-upstream-to=origin/master masterこのコマンドを実行すると、.git/config内に以下が追記される。
~/.git/config[branch "master"] remote = origin merge = refs/heads/masterマージ参照先が追記されていることが確認できる。
- 投稿日:2020-09-13T22:40:36+09:00
A sample of `git rebase`
Assumption
- Sample branches' status
/--- commit1--- commit2 `feature-1` / *----*----* `develop` \ \--- commitA --- commitB `feature-A`
- Changes in branch
feature-1
andfeature-A
have no conflictsDo a rebase
Case 1: On branch
feature-A
, rebase branchfeature-1
$ git checkout feature-A
$ git rebase feature-1
First, rewinding head to replay your work on top of it...
Applying: commitA
Applying: commitB
- Then the commit history will look like this:
/--- commitA --- commitB `feature-A ` / /--- commit1--- commit2 `feature-1` / *----*----* `develop`Case 2: On branch
feature-1
, rebase branchfeature-A
$ git checkout feature-1
$ git rebase feature-A
First, rewinding head to replay your work on top of it...
Applying: commit1
Applying: commit2
- Then the commit history will look like this:
/--- commit1 --- commit2 `feature-1 ` / /--- commitA--- commitB `feature-A` / *----*----* `develop`
- 投稿日:2020-09-13T22:40:36+09:00
git rebaseの例
Assumption
- Sample branches' status
/--- commit1--- commit2 `feature-1` / *----*----* `develop` \ \--- commitA --- commitB `feature-A`
- Changes in branch
feature-1
andfeature-A
have no conflictsDo a rebase
Case 1: On branch
feature-A
, rebase branchfeature-1
$ git checkout feature-A
$ git rebase feature-1
First, rewinding head to replay your work on top of it...
Applying: commitA
Applying: commitB
- Then the commit history will look like this:
/--- commitA --- commitB `feature-A ` / /--- commit1--- commit2 `feature-1` / *----*----* `develop`Case 2: On branch
feature-1
, rebase branchfeature-A
$ git checkout feature-1
$ git rebase feature-A
First, rewinding head to replay your work on top of it...
Applying: commit1
Applying: commit2
- Then the commit history will look like this:
/--- commit1 --- commit2 `feature-1 ` / /--- commitA--- commitB `feature-A` / *----*----* `develop`
- 投稿日:2020-09-13T22:35:32+09:00
GitHubコマンド集
はじめに
今回の記事は、GitHubのコマンドについてまとめました。
Gitはスナップショット?差分?
Gitは過去の履歴全てを記憶しているのでしょうか?それとも現在との差分のみ記憶しているのでしょうか?
答えは前者の過去の履歴全てを記憶しています。そして、コミットした記憶を辿ることで、以前の状態に戻ることができます。git clone
リモートリポジトリから、ローカルリポジトリにコピーする。
一回やればOK$ git clone URL #URLはGitHubからコピーできる。git pull
リモートリポジトリの変更点をローカルリポジトリに反映。
$ git pull origin master変更点を反映するまでの大まかな流れ
#ローカルリポジトリの作成 $ git init #変更点の確認 $ git status #変更差分の確認 $ git diff #反映させる変更を準備 #大砲の玉を用意 $ git add ~ #変更を記録する #大砲の玉をセット $ git commit ~ #変更を反映させる #大砲を発射して、変更を届ける $ git push ~git init
ローカルリポジトリを新規作成する。
$ git initgit status
現在の状態を確認する。
変更されているファイルを表示。$ git statusgit diff
変更差分を確認。
変更した詳細を表示。#git addする前の変更差分を確認 $ git diff $ git diff ファイル名 #git addした後の変更差分を確認 $ git diff --stagedgit add
コミットする変更を準備するところ。
大砲に玉を用意した状態。$ git add ファイル名 #ファイル名を指定 $ git add ディレクトリ名 #ディレクトリ名を指定 $ git add . #全部一気にやっちゃうgit commit
現在の状態をセーブ。
変更点などのメッセージを付けられる。
大砲の玉をセットした状態。
スナップショットとして記録することで、後から戻ることができる。$ git commit -m 'メッセージ' $ git commit -v #変更内容を見ることができるgit push
Gitにアップロードする。
大砲を発射!#これ以降urlを記載しなくて良くなる $ git remote add origin URL $ git push <リモート名><ブランチ名> #originはリモートサーバのこと #次回以降git pushでプッシュできるようにする $ git push -u origin mastergit log
コミット履歴を確認できる。
$ git log #1行で表示する $ git log --oneline #ファイルの変更差分を表示 $ git log -p <ファイル名> #表示するコミット数を制限 $ git log -n <コミット数>git rm
ファイルの記録の削除。
#ファイルごと記録を削除 $ git rm <ファイル名> $ gir rm -r <ディレクトリ名> #ファイルは残して記録を消したいとき $ git rm --cached <ファイル名>git mv
ファイル名の変更を記録する。
$ git mv <旧ファイル名><新ファイル名>git reset
addした後に変更を取り消す。
$ git reset HEAD <ファイル名> $ git reset HEAD <ディレクトリ名> # 全変更を取り消す $ git reset HEAD .直前のコミットをやり直す
ただし、リモートリポジトリにプッシュしたコミットはやり直してはいけない。
$ git commit --amendリモートリポジトリの情報を表示
設定しているリモートリポジトリの情報を表示する。
$ git remote # 対応するURLを表示 $ git remote -vリモートリポジトリを新規作成する
$ git remote add <リモート名> <リモートURL> $ git remote add tutorial URL #tutorialというショートカットでリモートリポジトリを登録ブランチ
gitの変更履歴を枝分かれさせる機能。
masterが本体で、ブランチは本体から伸びる絵だと想像してもらうとわかりやすいと思います。
2人以上で開発する場合、誰かが変更を加えたせいでバグなどが起きる可能性があります。
しかし、ブランチがあることにより、ブランチでいくら変更を加えても本体のmasterには影響しません。git checkout
ブランチを切り替えるコマンド。
$ git checkout -b <切替先のブランチ名> <切るブランチ名> $ git checkout -b branch master #masterブランチを切って、branchブランチに切り替える。または、ファイルへの変更を取り消す。
$ git checkout -- <ファイル名> $ git checkout -- <ディレクトリ名> # 全変更を取り消す $ git checkout -- .git branch
現在のブランチの確認
$ git branch * branch #こっちにいる master #全てのブランチを表示 $ git branch -agit merge
ブランチを合流させる。
#マージ先のブランチに移動 $ git checkout master #大体master $ git merge <マージしたいブランチ名>プルリクエスト
「このブランチをマスターにマージしてください」というお願い。
それに対するコメントやレビューがGitHub上でできる。$ git checkout -b <ブランチ名> origin/master $ git commit -m 'メッセージ' $ git push origin <ブランチ名>これが終わると、GiuHub上に「pull request」ボタンが現れる。
これを押すことで、プロリクエストができる。
お願いされた側は、問題がなければ「Merge pull request」でマージする。git fetch
リモートの状態をローカルにダウンロードしてくる。
git pullとの違いは、git pullの場合merge文字どうでしてくれるが、git fetchはmergeはしないという点です。$ git fetch originエイリアス
gitのコマンドを短くできます。
$ git config --global alias.<短縮名> <短縮したいものの名前> $ git config --global alias.cm commit注意点
同じブランチで作業して、それをリモートリポジトリにプッシュした場合、先にプッシュしたほうが優先され、後にプッシュした方ではエラーが出る。
最後に
中途半端なところで止まっているので、今後アップデートさせていきます。
おすすめ動画
Git入門】Git+Github使い方入門講座?Gitの仕組みやブランチの運用ルールまで、開発現場で必要な知識を完全解説!デザイナーやプログラマー必見のGit入門!
- 投稿日:2020-09-13T21:27:14+09:00
Git Fundamentals
Config git
Some git basic config
git config --global user.name "Nguyen Van A" git config --global user.email nguyen_van_a@example.com git config --global core.editor notepad++ git config --global help.autocorrect 1 git config --global core.autocrlf false # list all global config git config --global --listWorking locally with git
init - status - add - commit - log
Create a local repo
git initSee untracked files
git statusAdd (stage) a file to commit it
git add file_nameCommit the file
git commit -m "message"See commit log
git log
Add (stage) all modified (updated) files to commit
git add -uThen commit it to local repo
git commit -m "message"
Add (stage) all changed files (including new files)
git add -AThen commit them
git commit -m "message"
diff - show
See changes between 2 commits
git diff 1st_commit_number..2nd_commit_numberOr
git diff HEAD~1..[HEAD](see diff of HEAD and 1 commit before it)
Add (stage) a file to commit
git add file_nameSee staged changes
git diff --cached
See changes of a commit
git show commit_numberOr
git show HEAD
add - reset soft
Add (stage) a file to commit
git add file_nameBut want to unstage it after that
git reset --soft
checkout - branch
Create a new branch from the current branch
git checkout -b new_branch_nameSee available branches at local
git branchCheckout to another local branch
git checkout a_branchRename the current branch
git branch -m new_nameDelete a local branch
git branch -d branch_to_deletecheckout - reset hard
Checkout to remove uncommitted changes of a file to HEAD
git checkout file_nameReset to remove all uncommitted changes (of all files)
git reset --hardMove the HEAD back 1 commit
git reset --hard HEAD~1
merge
Merge a branch to the current branch
git merge the_branch_to_merge_to_current_branchAfter that delete the merged branch
git branch -d the_branch_merged_to_current_branch
Working remotely with git
Clone a repo
git clone url_of_the_repoSee commit history of the project
cd to_the_cloned_project git log
Check what is remoted
git remote -vfetch new branches... from remote repo
git fetchView remote branches
git branch -rCreate a local branch from a remote branch
git checkout remote_branch # Will create a local branch that tracks the remote branch
Make some changes then commit to the local branch (see Working locally with git)
Push committed content back to the remote branch
git pushPull changes of the remote branch to the local branch
git pullClean remote branches that have been deleted from the remote repo
git remote prune [remote_repo_name](remote_repo_name is something like
origin
)Further git
stash
- You are working on a branch
- You have some uncommited changes
- And the changes are not ready to commit
- Then you have to checkout another branch to fix something (that is urgent)
→ You can
stash
the changes for later commit
git stash
modified content will be saved into stash
git stash list
view what is being stashed
git stash apply stash_number
take out a stash to your code, but not remove from the stash list
git stash pop stash_number
take out a stash to your code, also remove it from the stash list
git stash drop stash_number
delete a stashcherry-pick
- You have commitA on a branchA
- You want to apply that commit onto another branchB (with the same changes)
→ You can
cherry-pick
commitA of branchA to branchBgit checkout branchB git cherry-pick commit_number_of_commitA
- 投稿日:2020-09-13T21:27:14+09:00
Gitの基礎
Config git
Some git basic config
git config --global user.name "Nguyen Van A" git config --global user.email nguyen_van_a@example.com git config --global core.editor notepad++ git config --global help.autocorrect 1 git config --global core.autocrlf false # list all global config git config --global --listWorking locally with git
init - status - add - commit - log
- Create a local repo
git init
- See untracked files
git status
- Add (stage) a file to commit it
git add file_nameCommit the file
git commit -m "message"
- See commit log
git log
- Add (stage) all modified (updated) files to commit
git add -u
- Then commit it to local repo
git commit -m "message"
- Add (stage) all changed files (including new files)
git add -A
- Then commit them
git commit -m "message"diff - show
- See changes between 2 commits
git diff 1st_commit_number..2nd_commit_number
- Or
git diff HEAD~1..[HEAD](to see diff of HEAD and 1 commit before it)
- Add (stage) a file to commit
git add file_name
- See staged changes
git diff --cached
- See changes of a commit
git show commit_number
- Or
git show HEADadd - reset soft
- Add (stage) a file to commit
git add file_name
- But want to unstage it after that
git reset --softcheckout - branch
- Create a new branch from the current branch
git checkout -b new_branch_name
- See available branches at local
git branch
- Checkout to another local branch
git checkout a_branch
- Rename the current branch
git branch -m new_name
- Delete a local branch
git branch -d branch_to_deletecheckout - reset hard
- Checkout to remove uncommitted changes of a file to HEAD
git checkout file_name
- Reset to remove all uncommitted changes (of all files)
git reset --hard
- Move the HEAD back 1 commit
git reset --hard HEAD~1merge
- Merge a branch to the current branch
git merge the_branch_to_merge_to_current_branch
- After that delete the merged branch
git branch -d the_branch_merged_to_current_branchWorking remotely with git
- Clone a repo
git clone url_of_the_repo
- See commit history of the project
cd to_the_cloned_project git log
- Check what is remoted
git remote -v
- fetch new branches... from remote repo
git fetch
- View remote branches
git branch -r
- Create a local branch from a remote branch
git checkout remote_branch # Will create a local branch that tracks the remote branch
Make some changes then commit to the local branch (see Working locally with git)
Push committed content back to the remote branch
git push
- Pull changes of the remote branch to the local branch
git pull
- Clean remote branches that have been deleted from the remote repo
git remote prune [remote_repo_name](remote_repo_name is something like
origin
)Further git
stash
If:
- You are working on a branch
- You have some uncommited changes
- And the changes are not ready to commit
- Then you have to checkout another branch to fix something (that is urgent)
→ You can
stash
the changes for later commit
git stash
modified content will be saved into stash
git stash list
view what is being stashed
git stash apply stash_number
take out a stash to your code, but not remove from the stash list
git stash pop stash_number
take out a stash to your code, also remove it from the stash list
git stash drop stash_number
delete a stashcherry-pick
If:
- You have commitA on a branchA
- You want to apply that commit onto another branchB (with the same changes)
→ You can
cherry-pick
commitA of branchA to branchBgit checkout branchB git cherry-pick commit_number_of_commitA
- 投稿日:2020-09-13T18:23:18+09:00
Gitのバージョンアップがうまく反映されない時の対応方法
gitのバージョンを調べる。
$ git --version git version 2.24.3 (Apple Git-128)Homebrewでgitをバージョンアップ(アップグレード)。
$ brew upgrade git Updating Homebrew... Warning: git 2.28.0 already installed既に最新になっているものの、
git --version
コマンドで確認するgitのバージョンは2.24.3
である。調べると、
git --version
を叩いた際に(Apple Git-128)
と出てくる場合、Xcode(コマンドラインツール)経由でインストールしたgitらしい。つまり、PC上には、
①Xcode(コマンドラインツール)経由でインストールしたgit(ver.2.24.3 (Apple Git-128) )
②Homebrew経由でインストールしたgit(ver.2.28.0)
が存在する。どちらのgitを使用するかPCに設定する必要(PATHを通す)がある。
zshを利用しているので、.zshrcファイル
にパスを記載する。$ vi ~/.zshrc
i
キーでINSERTモードにしてPATHを記載。export PATH=/usr/local/bin/git:$PATH
esc
キーでINSERTモードを終了し、:wq
で変更を保存ターミナルを再起動して、再度gitのバージョンを確認。
$ git --version git version 2.28.0Homebrew経由でインストールしたgitにPATHが通っていることを確認できた。
- 投稿日:2020-09-13T18:10:14+09:00
GitHub 備忘録
はじめに
Gitについて学んだはずなのに、いざ共同開発を始めようとしたときに自分が全然Gitを理解していないことに気が付いたので、備忘録として残すことにしました。
以下はGitHubに対する私個人の解釈が強く含まれていますので、何か誤解がありましたら遠慮なくご指摘ください。
開発環境
- Windows 10 Home
GitHubの目的
簡単に言うと、チームでディレクトリを管理することである。
ある時点でのディレクトリの状態のことをバージョンと呼び、それを共同で管理するプラットフォームがGitHub。そして、Gitはバージョンを管理するツールである。
ディレクトリのうち、Gitが接続されたものをリポジトリと呼ぶことにする。また、GitHub上に存在して協同でリポジトリをリモートリポジトリ、自分のpc上のリポジトリをローカルリポジトリと呼ぶことにする。
つまり、GitHubを用いたチーム開発とは、各個人がローカルリポジトリで作業をし、そしてそれぞれがその作業の結果をリモートリポジトリに反映させていくことである。
具体的な作業
1. 全体の作業を自分に反映
cloneとは、リモートリポジトリを自分のpcにコピーすること、すなわちローカルリポジトリを作ることである。繰り返しはなるが、リポジトリとは、Gitが接続されたディレクトリのことである。よって作業もこれをただのディレクトリとみなして進めればいい。
git clone <<リモートリポジトリのURL>>また、pullとは、既にリモートリポジトリをclone済みのときに、リモートリポジトリの内容を今のローカルリポジトリの内容(あるブランチについて)更新させることである。
git pull origin <<ブランチ名>>ここでいうoriginはリモートディレクトリを指している。
2. branchを切って作業
個人的にはこれはパラレルワールドを作っていくイメージである。
branchを切ることで異なる時間軸が用意できる。ローカルでの作業では、自分が進めたい時間軸を新たに作成してそこで作業をし、完成したらチームのメンバーに確認をしてもらって、メインの時間軸に自分の作業を反映させることができる。※注意:branchを切るためにはcdコマンドでそのディレクトリに移動をしておく必要がある。
- 新たなbranchを切ってそこに移動
git checkout -b ブランチ名branchの種類
- master branch
デフォルトのメインのbranch。これを完成形に持っていくのが目標。
- develop branchサブのbranch。ここまで共同で管理。確実に変更することがなさそうならmasterにpushする。ローカルでの作業のためにはさらにここからbranchを切っていく。
3. 自分の作業を全体に反映
ローカル(ワーキングツリーとも呼ばれる)で作業を進めたら、最終的にはそれは全体に共有させたい。すなわち、リモートリポジトリに作業を反映させたい。そのためには、
- 自分の作業をローカルリポジトリに反映させること
- ローカルリポジトリの内容をリモートリポジトリに反映させること
が必要である。
1. どのファイルをローカルリポジトリに反映させるかを決める(add)
どの作業をローカルリポジトリに反映させるかは選べるのである。選ばれたファイルたちがいる場所はステージやインデックスと呼ばれる。
git add <<ファイル名>>現在のディレクトリ以下にある、作業をしたすべてのファイルをステージに上げたければ以下のコマンドも使える。
git add .2. ローカルリポジトリを更新(commit)
インデックスの内容をローカルリポジトリに反映させる。
git commit -m "コミットメッセージ"3. リモートリポジトリに反映(push)
ローカルリポジトリの修正内容をリモートリポジトリに反映させる。
git push origin <<branch名>>考察・終わりに
新しい言葉がたくさん出てきてややこしいですが、実際にgithubを使いながら慣れていければと思っています。
- 投稿日:2020-09-13T13:21:25+09:00
gitのリポジトリをDropboxやGoogle Drive内に置きたくないとき
ローカルマシンの特定の場所をgit置き場にするスクリプト
Dropbox内にgitリポジトリを置きたくないときの方法
git init と引っ越し用スクリプト
git init --seprated-git-dir=XXX
の簡略化- 既にgit初期化れている場合はコピーして、.gitファイルで参照する
git_init.batecho off setlocal setlocal enabledelayedexpansion echo ---------------------- echo git initialize script echo ---------------------- echo init in %CD% rem ローカルgitまとめ場 set PATH_GIT_REPO=D:\git_repos set PATH_WORK=%CD% call :GET_FILENAME "%CD%" set PATH_TARGET_GIT_REPO=%PATH_GIT_REPO%%TARGET_PATH% echo git repository will put in "%PATH_TARGET_GIT_REPO%" pause rem git初期化されているか確認 echo checking git repository... if exist .git\ ( echo this directory has git repository set /P selected="replace git?(Y=YES / N=NO)?" if /i !selected!!==y ( goto git_replace ) else ( goto abort ) ) else ( if exist .git ( echo this directory has separated git repository goto abort ) else ( echo this directory does NOT have a git repository set selected=y set /P selected="git init here?(Y=YES / N=NO)?" if /i !selected!!==y ( goto git_init_separated ) else ( goto abort ) ) ) :git_init_separated echo git init separated in %PATH_TARGET_GIT_REPO% git init --separate-git-dir="%PATH_TARGET_GIT_REPO%" goto end :git_init_here echo git init here git init goto end :end echo finish pause exit /b 0 :abort echo script cancelled pause exit /b 0 rem 既存のリポジトリの引っ越し :git_replace echo replace git to "%PATH_TARGET_GIT_REPO%" rem フォルダを作成 rem パスが存在するか調べてなかったらつくる if exist %PATH_TARGET_GIT_REPO% ( echo %PATH_TAGET_GIT_REPO%% already exist set /P selected="overwrite them?(Y=YES / N=NO)?" if /i !selected!!==y ( goto git_copy ) else ( goto abort ) ) else ( mkdir "%PATH_TARGET_GIT_REPO%" echo make directory "%PATH_TARGET_GIT_REPO%" ) :git_copy rem .git内部をコピー echo copy .git to "%PATH_TARGET_GIT_REPO%" xcopy /d /e /y "%PATH_WORK%\.git" "%PATH_TARGET_GIT_REPO%" pause rem .gitフォルダを削除 echo delete origin .git files rmdir "%PATH_WORK%\.git" /s rem \を/に変更 echo make a reference .git file set GIT_DIR=%PATH_TARGET_GIT_REPO:\=/% set GIT_DIR=%GIT_DIR:"=% type nul > .git echo gitdir: "%GIT_DIR%"> .git goto end rem サブルーチン rem ドライブ名以下のパスを取得 :GET_FILENAME set TARGET_PATH=%~pn1 exit /b 0git clone用スクリプト
git_clone.batecho off setlocal setlocal enabledelayedexpansion echo ---------------------- echo git clone script echo ---------------------- echo git clone in %CD% set GIT_URL= set GIT_URL=%~1 if "%GIT_URL%"=="" ( echo empty argument goto abort ) else ( echo git url is %GIT_URL%% ) set GIT_NAME_PATH=%GIT_URL:/=\% call :GET_GIT_NAME "%GIT_NAME_PATH%" rem ローカルgitまとめ場 set PATH_GIT_REPO=D:\git_repos set PATH_WORK=%CD% call :GET_FILENAME "%CD%" rem パスが存在するか調べて作りなおすか確認 rem git cloneはすでにディレクトリがあると失敗する if exist %PATH_TARGET_GIT_REPO% ( echo %PATH_TAGET_GIT_REPO%% already exist set /P selected="overwrite them?(Y=YES / N=NO)?" if /i !selected!!==y ( rmdir /s /q "%PATH_TARGET_GIT_REPO%" echo remove dir "%PATH_TARGET_GIT_REPO%" goto git_clone_separated ) ) :git_clone_separated echo git clone separated in %PATH_TARGET_GIT_REPO% git clone %GIT_URL% --depth 1 --separate-git-dir="%PATH_TARGET_GIT_REPO%" goto end :end echo finish pause exit /b 0 :abort echo script cancelled pause exit /b 0 rem サブルーチン :GET_GIT_NAME set GIT_NAME=%~n1 echo git name is %GIT_NAME% exit /b 0 rem ドライブ名以下のパスを取得 :GET_FILENAME set TARGET_PATH=%~pn1 set PATH_TARGET_GIT_REPO=%PATH_GIT_REPO%%TARGET_PATH%\%GIT_NAME% echo git repository will put into "%PATH_TARGET_GIT_REPO%" exit /b 0
- 投稿日:2020-09-13T08:56:43+09:00
GoでExcelを操作する
Goでエクセルファイルを操作する方法のメモです。
tealeg/xlsxライブラリを使うと簡単でした。Gitのインポート
github上のライブラリを使用するため、Gitのダウンロードが必要でした。
※設定はデフォルトのままで行いました。ライブラリのインポート
コマンドプロンプトで以下のコマンドを実行すると、エクセルのライブラリが使えるようになります。go get の後のURIはGoDocで検索して取得しました。
go get github.com/tealeg/xlsxコマンドで実行するだけなので楽でした。
プログラム作成
テスト用として、エクセルにシートを追加するプログラムを作成しました。
<手順>
1. goファイルと同階層に新規ワークシートを作成
2. ファイル名を「テスト」とリネームして保存
3. 以下プログラムを実行test.gopackage main import ( "fmt" "time" "github.com/tealeg/xlsx" ) func main() { dt1 := time.Now() excel, _ := xlsx.OpenFile("テスト.xlsx") excel.AddSheet("GOで作成") excel.Save("./new.xlsx") dt2 := time.Now() fmt.Println(dt2.Sub(dt1)) // 実行時間 }実行すると、「テスト.xlsx」のコピーにシートが追加された状態で新しいファイル「new.xlsx」が同階層に保存されます。
※はじめは上書きをするプログラムを作成したのですが、PC内のセキュリティソフトに「疑わしい処理」としてブロックされてしまいました。上書きするには、エクセルシートのセキュリティの設定を下げる必要がありそうです。プログラムの実行時間は12ミリ秒でした。
エクセルファイルを開いて、自身でシートを追加するより圧倒的に速いです。使用関数
tealeg/xlsxライブラリ内から以下の関数を使用しました。
func OpenFile(fileName string, options ...FileOption) (file *File, err error)
OpenFile will take the name of an XLSX file and returns a populated xlsx.
File struct for it. You may pass it zero, one or many FileOption functions that affect the behaviour of the file.func (f *File) AddSheet(sheetName string) (*Sheet, error)
AddSheet Add a new Sheet, with the provided name, to a File.
The minimum sheet name length is 1 character.
If the sheet name length is less an error is thrown.
The maximum sheet name length is 31 characters.
If the sheet name length is exceeded an error is thrown.
These special characters are also not allowed: : \ / ? * [ ]func (f *File) Save(path string) (err error)
Save the File to an xlsx file at the provided path.
今回はファイルやシートを扱う関数を使用しましたが、セルや列を扱う関数もあるようなので、
もう少し便利なプログラムを作れそうです。
▼参考にしたサイト
Go言語でExcelファイルを処理するのが超簡単だった
- 投稿日:2020-09-13T08:16:08+09:00
GitHubにある Poetry プロジェクトを参照するサンプル
はじめに
PipenvとPoetryの一番の大きな違いはPipenvはライブラリプロジェクトでは使えないが、Poetryは使えるところではと思い、Poetryを勉強しました。
mainプロジェクトとlibraryプロジェクトの2つのプロジェクトをGithub経由でインストールしてlibraryプロジェクトの関数をmainプロジェクトから実行するサンプルです。
ソースは mainプロジェクトと libraryプロジェクト にあげてあります。
ディレクトリ構成
poetry-lib├── pyproject.toml └── poetry_lib └── aaa2.pypoetry-main├── pyproject.toml └── main.pyファイル
poetry-lib
pyproject.toml[tool.poetry] name = "poetry-lib" version = "0.1.0" description = "" authors = ["va034600"] packages = [ { include = "poetry_lib" }, ] [tool.poetry.dependencies] python = "^3.6" python-dateutil = "^2.8.1" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api"aaa2.pyfrom dateutil.relativedelta import * from datetime import * def bbb2(): today = datetime.now() return today + relativedelta(months=+6)poetry-main
pyproject.toml[tool.poetry] name = "poetry-main" version = "0.1.0" description = "" authors = ["va034600"] [tool.poetry.dependencies] python = "^3.6" poetry-lib = { git = "ssh://git@github.com/va034600/poetry-lib.git" } [tool.poetry.dev-dependencies] [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api"main.pyfrom poetry_lib import aaa2 print(aaa2.bbb2())実行
setup$ cd poetry-main $ poetry install $ python main.py 2021-03-13 08:00:14.266597終わりに
Pytharmを使えば、mainプロジェクトにlibraryプロジェクトをアタッチすればデバッグもlibraryを修正した反映の確認もできます。
便利です
- 投稿日:2020-09-13T08:16:08+09:00
GitHubにある Poetry プロジェクトのサンプル
はじめに
PipenvとPoetryの一番の大きな違いはPipenvはライブラリプロジェクトでは使えないが、Poetryは使えるところではと思い、Poetryを勉強しました。
mainプロジェクトとlibraryプロジェクトの2つのプロジェクトをGithub経由でインストールしてlibraryプロジェクトの関数をmainプロジェクトから実行するサンプルです。
ソースは mainプロジェクトと libraryプロジェクト にあげてあります。
ディレクトリ構成
poetry-lib├── pyproject.toml └── poetry_lib └── aaa2.pypoetry-main├── pyproject.toml └── main.pyファイル
poetry-lib
pyproject.toml[tool.poetry] name = "poetry-lib" version = "0.1.0" description = "" authors = ["va034600"] packages = [ { include = "poetry_lib" }, ] [tool.poetry.dependencies] python = "^3.6" python-dateutil = "^2.8.1" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api"aaa2.pyfrom dateutil.relativedelta import * from datetime import * def bbb2(): today = datetime.now() return today + relativedelta(months=+6)poetry-main
pyproject.toml[tool.poetry] name = "poetry-main" version = "0.1.0" description = "" authors = ["va034600"] [tool.poetry.dependencies] python = "^3.6" poetry-lib = { git = "ssh://git@github.com/va034600/poetry-lib.git" } [tool.poetry.dev-dependencies] [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api"main.pyfrom poetry_lib import aaa2 print(aaa2.bbb2())実行
setup$ cd poetry-main $ poetry install $ python main.py 2021-03-13 08:00:14.266597終わりに
Pytharmを使えば、mainプロジェクトにlibraryプロジェクトをアタッチすればデバッグもlibraryを修正した反映の確認もできます。
便利です