Public
Edited
Apr 30, 2024
Insert cell
Insert cell
Insert cell
workbook = FileAttachment("March_wk3.xlsx").xlsx()
Insert cell
workbook.sheetNames
Insert cell
data = workbook.sheet(0, {
headers: false,
// range: "A1:J10"
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
counts = Array(intervals).fill(0); // Initialize an array for counts
Insert cell
Insert cell
Insert cell
Insert cell
viewof myHtml = html`<div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When do you </p>
</div>`
Insert cell
Insert cell
Insert cell
function extractDuration(data) {
const durations = {};
// 遍历原始数据
data.forEach(item => {
const date = item.A; // 假设 A 列表示日期
const mealType = item.C; // 假设 C 列表示吃饭类型
const duration = item.L; // 假设 L 列表示时长

// 如果当前日期不存在于 durations 对象中,则创建一个新条目
if (!durations[date]) {
durations[date] = { date, breakfast: 0, lunch: 0, dinner: 0 };
}

// 根据餐点类型将时长添加到对应餐点上
durations[date][mealType.toLowerCase()] += duration;
});

// 将对象转换为数组形式,并返回
const durationArray = Object.values(durations);
return durationArray;
}

Insert cell
duration_time = extractDuration(data)
Insert cell
transformedData = duration_time.flatMap(d => [
{ day: d.date, meal: 'breakfast', value: d.breakfast },
{ day: d.date, meal: 'lunch', value: d.lunch },
{ day: d.date, meal: 'dinner', value: d.dinner },
]);
Insert cell
function getDayOfWeek(dateString) {
const date = new Date(dateString);
const dayOfWeek = date.getDay(); // getDay() 返回 0 (周日) 到 6 (周六)
return ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'][dayOfWeek];
}
Insert cell
dataWithDayOfWeek = transformedData.map(d => ({
...d,
day: getDayOfWeek(d.day)
}));
Insert cell
Insert cell
Insert cell
Insert cell
workbook1 = FileAttachment("March3.xlsx").xlsx()
// workbook1 = FileAttachment("month2.xlsx").xlsx()
Insert cell
workbook1.sheetNames
Insert cell
data1 = workbook1.sheet(0, {
headers: false,
// range: "A1:J10"
})
Insert cell
data1
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more