1

In the following code, I have a Dropdown and a Button. Once you select an option in the Dropdown and click the Button, that value will be sent to Excel in the active cell. After sending this data to Excel, can I set the Dropdown back to the placeholder? Or can I set the Dropdown to the first value (which in this case is blank)? How can I set the Dropdown value back to placeholder or blank?

import * as React from "react";
import { Dropdown, DropdownMenuItemType, IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown';
import { PrimaryButton } from 'office-ui-fabric-react/lib/';
export interface ParentProps {
};
export interface ParentState  {
  selectedItem?: { key: string | number | undefined };
  operationType?;
};
export default class ParentComponent extends React.Component<ParentProps, ParentState> {
  constructor(props, context) {
    super(props, context);
    this.state = {
      operationType: '',
    };
  }
  addToExcel = async () => {
    try {
      await Excel.run(async context => {
        const range = context.workbook.getSelectedRange();
        range.load("address");
        await context.sync();
        range.values = (this.state.operationType);
      });
    } catch (error) {
      console.error(error);
    }
    this.setState({
    })
  };
  render() {
    const { selectedItem } = this.state;
    const options: IDropdownOption[] = [
      { key: 'blank', text: '' },
      { key: 'topLevelMake', text: 'Parents', itemType: DropdownMenuItemType.Header },
      { key: 'topLevel', text: 'TOP LEVEL' },
      { key: 'make', text: 'MAKE ITEM' },
    ];
    return (
      <div>
        <Dropdown
          label="Operation"
          selectedKey={selectedItem ? selectedItem.key : undefined}
          onChange={this._onChange}
          placeholder={"Select an option"}
          options={options}
          styles={{ dropdown: { width: 300 } }}
        />
        <p></p>
        <PrimaryButton
          text="Enter"
          onClick={this.addToExcel}
        />
      </div>
    );
  }
  private _onChange = (e, item: IDropdownOption): void => {
    this.setState({ selectedItem: item });
    this.setState({ operationType: item.text })
    console.log(e);
    }
  };
Ethan
  • 808
  • 3
  • 21

2 Answers2

2

Try something like this on your addToExcel():

  addToExcel = async () => {
    try {
      await Excel.run(async context => {
        const range = context.workbook.getSelectedRange();
        range.load("address");
        await context.sync();
        range.values = (this.state.operationType);
      });
    } catch (error) {
      console.error(error);
    }
    this.setState({
      selectedItem: {key:'blank'},
    })
  };

Ethan
  • 808
  • 3
  • 21
0

You should update your state after your excel operations.

addToExcel = async () => {
    try {
      await Excel.run(async context => {
        const range = context.workbook.getSelectedRange();
        range.load("address");
        await context.sync();
        range.values = (this.state.operationType); 
      });
      // update state after asyn operations is done
        this.setState({
            selectedItem:undefined
        })
    } catch (error) {
      console.error(error);
    }

  };
Sarthak Aggarwal
  • 2,284
  • 1
  • 8
  • 12
  • hey Sarthak thanks for the response. Please see the updated code, I added what you told me to and my Dropdown does not change. – Ethan Apr 07 '20 at 18:00