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 hidenorihide777/abe17492301922de6df15d986c438a00 to your computer and use it in GitHub Desktop.
Save hidenorihide777/abe17492301922de6df15d986c438a00 to your computer and use it in GitHub Desktop.
function writeDataToSS(data){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('シート1');
Logger.log(data.length)
for (let raw=1;raw<=data.length;raw++){
sheet.getRange(raw+1,1).setValue(data[raw-1][0])
if (data[raw-1][1]!='0'){
sheet.getRange(raw+1,2).setValue(data[raw-1][1])
sheet.getRange(raw+1,3).setValue(data[raw-1][2])
}else{
sheet.getRange(raw+1,2).setValue("")
sheet.getRange(raw+1,3).setValue("")
}
}
}
function ArraySort(arr) {
arr.sort((a, b) => {
if (a[0] > b[0]) {
return 1;
} else {
return -1;
}
});
return arr
}
function myFunction(){
DATABASE_ID = '{DATABASE_ID}';
NOTION_API_KEY = '{MY_NOTION_TOKEN}';
const headers = {
'Authorization' : `Bearer ${NOTION_API_KEY}`,
'Notion-Version' : '2021-08-16'
};
const options = {
method: 'POST',
contentType : "application/json",
headers: headers,
//payload : JSON.stringify(filter),
};
const resp = UrlFetchApp.fetch(`https://api.notion.com/v1/databases/${DATABASE_ID}/query`, options);
const respObj = JSON.parse(resp);
var data = [];
var data_sorted = {}
var num = 0;
for (const item of respObj.results){
//Logger.log(item.properties)
//日付の取得
data.push(Array(3).fill(0));
if(item.properties["日付"]!=null){
data[num][0]=item.properties["日付"].date.start
}
//体重の取得
if(item.properties["体重"].rich_text[0]!=null){
data[num][1]=item.properties["体重"].rich_text[0].plain_text
}
//体脂肪率の取得
if(item.properties["体脂肪率"].rich_text[0]!=null){
data[num][2]=item.properties["体脂肪率"].rich_text[0].plain_text
}
//Logger.log(item.properties)
//writeDataToSS(data,num)
//
num = num + 1
}
data_sorted = ArraySort(data)
Logger.log(data_sorted)
writeDataToSS(data_sorted)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment