Google Apps Script でCypressのコードを生成する（まだ途中）

# Google Apps Script でCypressのコードを生成する（まだ途中）

1.スプレッドシートを作成する
2.1のスプレッドシートの1行目に以下を記載する

```
No	Summary	Url	Screen	Area1	Component	Procedure	Method	Path	PathAlias	TitleShould	InputParam	WaitMillSec	Code
```

3.以下のコードをGoogle Apps Scriptとして保存する

```
//https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet

const newLineChar = "\n";

function readSheet() {
  const spreadSheetId = "【スプレッドシートID】";
  const sheetName = "【シート名】";
  let spreadSheet = SpreadsheetApp.openById(spreadSheetId);
  let spreadSheetName = spreadSheet.getName();
  let nameSheet = spreadSheet.getSheetByName(sheetName);
  //                             row, col, row length, col length
  var sheetData = nameSheet.getRange(1, 1, 200, 14).getValues();
  console.log("range");
//  rangeValues.forEach(function(value, index){
//      console.log(index);
//      console.log(value);
//  });
  
  var lines = [];
  var header = sheetData[0];
  sheetData = sheetData.slice(1);
  sheetData.forEach(function(values){
    var line = {};
    values.forEach(function(value, index){
      line[header[index]] = value;
    });
    lines.push(line);
  });
  Logger.log(lines);
  
  let script = getHeaderInitScript(spreadSheetName, sheetName);
  const newLineChar = "\n";
  lines.forEach(function(value, index){
    Logger.log(value["Summary"]);
    script += Utilities.formatString("//No: %s; Summary: %s", value["No"], value["Summary"]) + newLineChar;
    script += getCommandsFromProcedure(value["Procedure"], value) + newLineChar;
  
  });
  script += getFooterInitScript();
  Logger.log(script);
  createFile(script);
}

function createFile(script) {
  const folderId = "【Google Drive上の出力フォルダのID】";
  const folder = DriveApp.getFolderById(folderId);

  const fileName = "cypress_code.ts";
  const contentType = "application/x-typescript"; //"plain/text";
  const charSet = "utf-8";
  var blob = Utilities.newBlob("", contentType, fileName).setDataFromString(script, charSet);
  folder.createFile(blob);
}

function getHeaderInitScript(spreadSheetName, sheetName){
  let script = Utilities.formatString("describe('%s', () => {", spreadSheetName) + newLineChar;
  script += Utilities.formatString("it('%s', () => {", sheetName) + newLineChar;
  return script;
}

function getCommandsFromProcedure(procedure, value){
  let command = "";
  switch(procedure){
    case "redirect":
      command += Utilities.formatString("cy.visit('%s');",value["Url"]);
      break;
    case "should_title":
      command += Utilities.formatString("cy.title().should(\"contain\", \"%s\");",value["TitleShould"]);
      break;
      
    case "click":
      command += Utilities.formatString("cy.get(\"%s\").click();",value["Component"]);
      break;

    case "alias":
      command += Utilities.formatString("cy.route(\"%s\", \"%s\").as(\"%s\");",value["Method"], value["Path"], value["PathAlias"]);
      break;

    case "wait_ms":
      command += Utilities.formatString("cy.wait(%s);",value["WaitMillSec"]);
      break;

    case "wait_request":
      command += Utilities.formatString("cy.route(\"%s\", \"%s\").as(\"%s\");",value["Method"], value["Path"], value["PathAlias"]);
      command += Utilities.formatString("cy.wait(\"@%s\", { timeout: 10000 }).then((xhr) => {",value["PathAlias"]);
      command += Utilities.formatString("if (xhr.status == 200) {");
      command += Utilities.formatString("cy.log(\"Success \");");
      command += Utilities.formatString("}");
      command += Utilities.formatString("});");
      break;

    case "type":
      command += Utilities.formatString("cy.get('%s').type('%s')",value["Component"], value["InputParam"]);
      break;
      
    case "code":
      let raw_code = value["Code"]
      command += raw_code;
      break;
      
    default:
      break;
  }
  
  return command;
}

function getFooterInitScript(){
  let script = "});" + newLineChar;
  script += "});" + newLineChar;
  return script;
}
```