LoginSignup
4
5

Office Scriptsチートシート

Last updated at Posted at 2023-12-14

Officeスクリプト側に引数を足した時のクラウドフローの扱い方

Excelコネクタのスクリプトの実行アクションを開いたときに引数が表示されればいいですが、想定と結果が異なった(エラーを想定していたのに正常終了した。Excel側に追加した引数を認識しているか確認するつもりだったが存在しない扱いで正常終了した)ことがあるので、5~10分置いてからワークフローを修正してテスト実行するようにすると安定すると思います。

指定したシートをアクティブにする

sheet.activate();で動作しました。

SPOリストの選択肢(複数選択可)の値を加工したい

こちらで紹介されている方法を試したのですが、どうしても自力でPower Automate側の型不一致のエラーを解消できませんでした。
こちらではPower AutomateではなくOffice Scriptに渡して処理をさせたほうが簡単だ、とあり試してみました。

1つの文字列引数で複数選択結果をOffice Scriptに引き渡す➡断念

文字列から配列への変換の仕方についてさらにこちらを参照しましたが手元では掲載されていないエラーが発生し、デバッグに時間がかかるので断念しました。

文字列の引数N個作成し、Office Script側で配列に追加する➡成功

ベタ書きですが上手くいきました。動けばそれが正。。

function main(
  workbook: ExcelScript.Workbook,
  your-argument: string,
  your-argument2: string,
  your-argument3: string,
  your-argument4: string
  ) 
{
    // 指定したワークシートをアクティブ化
    /** 対象シート名 */
    const sheetname = 'your-sheet';
    /** 対象ワークシート */
    const sheet = workbook.getWorksheet(sheetname);
    // 引数4つを配列に追加する 
    let your-obj: string[] = [];
    your-obj.push(your-argument1);
    your-obj.push(your-argument2);
    your-obj.push(your-argument3);
    your-obj.push(your-argument4);
    // ワークシートをアクティブ化
    sheet.activate();
    // 指定したセルのプロパティを編集する
    let selectedsheet = workbook.getActiveWorksheet();
   /** control */
    for (let index = 0; index < your-obj.length  ; index++) {
      switch (your-obj[index]) {
        case '0': //hoge1
          selectedsheet.getRange("C47").setValue("〇");
          break;
        case '1': //hoge2
          selectedsheet.getRange("C48").setValue("〇");
          break;
        case '2': //hoge3
          selectedsheet.getRange("C49").setValue("〇");
          break;
        case '3': //hoge4
          selectedsheet.getRange("C50").setValue("〇");
          break;
        default:
          break;
      }
}

Excelテーブルの全行削除

テーブルの全行削除をしたい場合にPower Automateのアクションでは全行削除ができませんでした。以下のスクリプトを「スクリプトの実行」アクションを使うことで自動削除が可能でした。Truncateをしたいケースで使えます。もちろんExcelのリボン>自動化>この削除スクリプトを選択して右側のペインから実行でも消すことができます。

function main(workbook: ExcelScript.Workbook) {
    let table = workbook.getTable("your-excel-table-name");
    let tableRange = table.getRangeBetweenHeaderAndTotal();
    // errorhandling if the table is already empty
    if (tableRange.getRowCount() == 1) {
        console.log("nothing to delete");
    }
    else {
        // delete all the cells in the table data range, the table range is then reset automatically
        tableRange.delete(ExcelScript.DeleteShiftDirection.up);
    }
}

型注釈でAny型宣言のエラーを回避

Excelで既にある表の前方に2列追加し、行番号と文字列を設定する処理を作ろうとしました。
テストしたところ引数に入る値の型が一致していないといけない、というような警告が出て以下のスクリプトで正常に動作しました。(ワーニングメッセージは残りっぱなしです)

  // Create a new array to hold the new column values
  // ----- 型注釈でAny型宣言のエラーを回避 -----
  let newColumnValues: (string | number)[][] = [];

  // Assume the "Message" column is now column I
  for (var i = 1; i < values.length; i++) { // Start from 1 to skip the header row
    // Set the row number in the first new column
    let rowNumber = i;

    // Set the value in the second new column based on the "Message" column
    // --- Property 'includes' does not exist on type 'string | number | boolean'. 
    // --- Property 'includes' does not existon type 'number'. の警告メッセージは無視でOK(文字列が入ることが保証できる)
    let messageValue = "";
    if (values[i][8].includes("start")) {
      messageValue = "start";
    } else if (values[i][8].includes("end")) {
      messageValue = "end";
    }

    newColumnValues.push([rowNumber, messageValue]);
  }

付録

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5