首页 > 其他分享 >Script Lab

Script Lab

时间:2023-08-18 14:11:26浏览次数:32  
标签:sheet Script expensesTable Lab add context 2017 weekendFormula

$("#setup").click(() => tryCatch(setup));

$("#add-row").click(() => tryCatch(addRow));
$("#add-column").click(() => tryCatch(addColumn));
$("#add-calculated-column").click(() => tryCatch(addCalculatedColumn));

async function addRow() {
  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const expensesTable = sheet.tables.getItem("ExpensesTable");

    expensesTable.rows.add(null, [
      ["1/16/2017", "THE PHONE COMPANY", "Communications", "$120"],
      ["1/20/2017", "NORTHWIND ELECTRIC CARS", "Transportation", "$142"],
      ["1/20/2017", "BEST FOR YOU ORGANICS COMPANY", "Groceries", "$27"]
    ]);

    sheet.getUsedRange().format.autofitColumns();
    sheet.getUsedRange().format.autofitRows();

    await context.sync();
  });
}

async function addColumn() {
  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const expensesTable = sheet.tables.getItem("ExpensesTable");

    expensesTable.columns.add(null, [
      ["Deductable?"],
      ["Yes"],
      ["Yes"],
      ["No"],
      ["No"],
      ["Yes"],
      ["Yes"],
      ["No"],
      ["Yes"],
      ["Yes"],
      ["No"]
    ]);

    sheet.getUsedRange().format.autofitColumns();
    sheet.getUsedRange().format.autofitRows();

    await context.sync();
  });
}

async function addCalculatedColumn() {
  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const expensesTable = sheet.tables.getItem("ExpensesTable");

    const weekendFormula =
      '=IF(OR((TEXT([@DATE], "dddd") = "Saturday"), (TEXT([@DATE], "dddd") = "Sunday")), "Weekend", "Weekday")';
    expensesTable.columns.add(null, [
      ["Type of the Day"],
      [weekendFormula],
      [weekendFormula],
      [weekendFormula],
      [weekendFormula],
      [weekendFormula],
      [weekendFormula],
      [weekendFormula],
      [weekendFormula],
      [weekendFormula],
      [weekendFormula]
    ]);

    sheet.getUsedRange().format.autofitColumns();
    sheet.getUsedRange().format.autofitRows();

    await context.sync();
  });
}

/** Create a new table with sample data */
async function setup() {
  await Excel.run(async (context) => {
    context.workbook.worksheets.getItemOrNullObject("Sample").delete();
    const sheet = context.workbook.worksheets.add("Sample");

    const expensesTable = sheet.tables.add("A1:D1", true /*hasHeaders*/);
    expensesTable.name = "ExpensesTable";
    expensesTable.getHeaderRowRange().values = [["Date", "Merchant", "Category", "Amount"]];

    expensesTable.rows.add(null /*add at the end*/, [
      ["1/1/2017", "The Phone Company", "Communications", "$120"],
      ["1/2/2017", "Northwind Electric Cars", "Transportation", "$142"],
      ["1/5/2017", "Best For You Organics Company", "Groceries", "$27"],
      ["1/10/2017", "Coho Vineyard", "Restaurant", "$33"],
      ["1/11/2017", "Bellows College", "Education", "$350"],
      ["1/15/2017", "Trey Research", "Other", "$135"],
      ["1/15/2017", "Best For You Organics Company", "Groceries", "$97"]
    ]);

    sheet.getUsedRange().format.autofitColumns();
    sheet.getUsedRange().format.autofitRows();

    sheet.activate();
    await context.sync();
  });
}

/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
    console.error(error);
  }
}
View Code

前端:

<section class="ms-font-m">
    <p>This sample shows how to add columns and rows to a table.</p>
</section>

<section class="setup ms-font-m">
    <h3>Set up</h3>
    <button id="setup" class="ms-Button">
        <span class="ms-Button-label">Create table</span>
    </button>
</section>

<section class="samples ms-font-m">
    <h3>Try it out</h3>
    <p>Press the following buttons in order, so rows and columns of appropriate sizes are added.</p>
    <button id="add-row" class="ms-Button">
            <span class="ms-Button-label">Add rows</span>
    </button>
    <p>
        <button id="add-column" class="ms-Button">
        <span class="ms-Button-label">Add a column</span>
    </button>
        <p>
            <button id="add-calculated-column" class="ms-Button">
        <span class="ms-Button-label">Add a calculated column</span>
    </button>
</section>
View Code

css:

section.samples {
    margin-top: 20px;
}

section.samples .ms-Button, section.setup .ms-Button {
    display: block;
    margin-bottom: 5px;
    margin-left: 20px;
    min-width: 80px;
}
View Code
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js

[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css

[email protected]/client/core.min.js
@types/core-js

[email protected]
@types/[email protected]
View Code

 

 

标签:sheet,Script,expensesTable,Lab,add,context,2017,weekendFormula
From: https://www.cnblogs.com/shiningleo007/p/17640339.html

相关文章

  • TypeScript学习
    TypeScript快速入门JavaScript是一种属于网络的高级脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美欢的浏览效果。TypeScript是JavaScript的一个超集,它扩展了JavaScript的语法通过在JavaScript的基础上添加静态类型定义......
  • 2 JavaScript的基础类型
    2JavaScript的基础类型JS虽然是一个脚本语言.麻雀虽小,五脏俱全.在js中也是可以像其他编程语言一样.声明变量,条件判断,流程控制等等.我们先看一下JS中的数据类型在js中主要有这么几种数据类型(基本)number数字,不论是整数还是小数,数据类型都是numberstring字......
  • 解决confluence协同编辑collaborative editing关闭后无法启动的故障
    环境背景因一个老confluence7.0在编辑页面缓慢提示加载时间长问题,百度了一下,尝试在“设置”>“一般配置”>“协同编辑”的页面关闭重启一下协同编辑,但是关闭协同编辑后,看似临时解决了这个问题,却导致了无法再次开启协同编辑了,因为协同编辑是confluence这种wiki的应该有的一个......
  • gitlab+jenkins+harbor+k8s部署微服务环境
    一、gitlab1.gitlb部署version:'3'services:gitlab:image:'gitlab/gitlab-ce:latest'container_name:'gitlab'restart:alwayshostname:'192.168.1.188'#部署机器的ip,非容器ip(因为是本地不是线上所以用ip,线上的话可以用域名)......
  • echart动态修改每个数据的label
    echart可以动态修改每个数据的label代码如下:data:type=='01'?this.yList[0].data.map((item,index)=>{console.log(item,'11111');this.yList[1].data.map((res,it)=>{if(index===it){......
  • QtWebChannel和JavaScript进行通信(简单理解)
    说明在使用Qt(C++)和JavaScript之间实现通信时,通常会使用一些模块和技术来使两者能够交互和传递数据。这种通信通常用于在Qt应用程序中嵌入Web内容,或者在Web页面中嵌入Qt应用程序。以下是一些常用的模块和技术,以及它们的作用QtWebEngine模块:作用:QtWebEngine是Qt中的Web引擎,允......
  • m基于FFT傅里叶变换的256QAM基带信号频偏估计和补偿FPGA实现,含testbench和matlab星座
    1.算法仿真效果本系统进行了Vivado2019.2平台的开发,并使用matlab2022a对结果进行星座图的显示:     频偏基带256qam信号和频偏补偿后的256qam基带信号使用matlab显示星座图,结果如下:   2.算法涉及理论知识概要         FFT傅里叶变换是一种高效的......
  • 【愚公系列】2023年08月 WPF控件专题 Label、TextBox、PasswordBox控件介绍
    (文章目录)前言WPF控件是WindowsPresentationFoundation(WPF)中的基本用户界面元素。它们是可视化对象,可以用来创建各种用户界面。WPF控件可以分为两类:原生控件和自定义控件。原生控件是由Microsoft提供的内置控件,如Button、TextBox、Label、ComboBox等。这些控件都是WPF中常见......
  • Matlab灰狼算法(GWO)优化双向长短期记忆神经网络的数据分类预测,GWO-BiLSTM分类预测,多
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • 路径规划算法:基于郊狼算法的机器人路径规划算法- 附matlab代码
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......