Skip to content

Instantly share code, notes, and snippets.

@NbtKmy
Last active September 6, 2019 07:06
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 NbtKmy/9baf7a6c4c492f2a00e465fa7d49d699 to your computer and use it in GitHub Desktop.
Save NbtKmy/9baf7a6c4c492f2a00e465fa7d49d699 to your computer and use it in GitHub Desktop.
get bibliography from zotero
function getBibliografy() {
//最後のスライドを取得し、そのテキストボックスも取得
var presentationID = "your presentation-id";
var presentation = SlidesApp.openById(presentationID);
var slide = presentation.getSlides()[x]; //スライドx枚目
var shape = slide.getShapes();
var textBox = shape[0]; // 1番目のShape
var textRange = textBox.getText();
textRange.clear();
//文献リスト取得
var zotero_api = "https://api.zotero.org/groups/yourGroupID/collections/yourCollectionId/items?format=bib";
var opt = {"contentType":"application/xml;","method":"get"};
var newBibl = UrlFetchApp.fetch(zotero_api, opt).getContentText("UTF-8");
//文献リストテキスト調整
var text1 = newBibl.replace(/“/g, '\"'); //“ を " へ
var text2 = text1.replace(/”/g, '\"'); //” を " へ
var text3 = text2.replace(/&/g, '\&'); //& を & へ
var text4 = text3.replace(/ü/g, 'ü'); // für uウムラウト 小文字
var text5 = text4.replace(/ä/g, 'ä'); // für aウムラウト 小文字
var frequency = text5.match(/<i>/g); //文字列「<i>」の出現回数=イタリック体となるべきタイトルの数
//タイトルをイタリック体として表示
for(var i = 0; i < frequency.length; i++){
var j = text5.indexOf("<i>");
if(j>=0){
var j_last = text5.indexOf("</i>");
var part = text5.slice(j+3, j_last); //この部分をイタリック体にする
var part_before = text5.slice(0, j);
var part_before_2 = part_before.replace(/<.*?>/g,""); //<>で囲まれた文字を削除
var part_after = text5.slice(j_last+4, text5.length);
if(i == 0){ //一周目はsetText()で
var firstpart = textRange.setText(part_before_2);
firstpart.getTextStyle()
.setItalic(false);
var InsertedText1 = textRange.appendText(part);
InsertedText1.getTextStyle()
.setItalic(true);
text5 = part_after;
}
else { //それ以降はappendTextで
var InsertedText_bef = textRange.appendText(part_before_2);
InsertedText_bef.getTextStyle()
.setItalic(false);
var InsertedText2 = textRange.appendText(part);
InsertedText2.getTextStyle()
.setItalic(true);
text5 = part_after;
}
}
}
var part_after_2 = part_after.replace(/<.*?>/g,""); //<>で囲まれた文字を削除
var InsertedText2 = textRange.appendText(part_after_2);
InsertedText2.getTextStyle()
.setItalic(false);
}
@NbtKmy
Copy link
Author

NbtKmy commented Sep 5, 2019

グーグルスライドのあるシェイプに対して、ZoteroのAPIから呼び出した文献リストを挿入します。
日本語には未対応。(文字化けします)
また、ウムラウトなどの文字への対応も未完全です。
適宜修正して使ってください。

また、Zoteroのコレクションは公開してある、という前提です。非公開の場合はさらにアクセストークンなどのコードを付け加える必要があると思います。

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