diff --git a/plugin/kintone-addins/src/actions/insert-value.ts b/plugin/kintone-addins/src/actions/insert-value.ts index c93d8f2..1fa0335 100644 --- a/plugin/kintone-addins/src/actions/insert-value.ts +++ b/plugin/kintone-addins/src/actions/insert-value.ts @@ -1,5 +1,6 @@ +import { each } from "jquery"; import { actionAddins } from "."; import { IAction,IActionResult, IActionNode, IActionProperty, IField, IContext } from "../types/ActionTypes"; import { ConditionTree } from '../types/Conditions'; @@ -41,9 +42,13 @@ export class InsertValueAction implements IAction{ * @param {string} inputValue - 挿入する値 * @return {boolean} -入力値が有効な日付形式の場合はtrueを返し、そうでない場合は例外を発生させる */ - checkInputBlank(fieldType :string | undefined,inputValue :string,fieldCode :string,fieldRequired :boolean | undefined,event :any): boolean{ + checkInputValueBlank(fieldType :string | undefined,inputValueArray :any,fieldCode :string,fieldRequired :boolean | undefined,event :any): boolean{ + + let blankCheck; //正規表現チェック - let blankCheck= inputValue.match(/^(\s| )+?$/);//半角スペース・タブ文字・改行・改ページ・全角スペース + for(let i =0;i{ + async setInputUser(inputValue :any): Promise{ //ユーザー名を格納する変数 - let usersName; - const usersInfoColumnIndex=0; + let usersName = []; try{ //APIでユーザー情報を取得する - const resp =await kintone.api(kintone.api.url('/v1/users', true), 'GET', {codes:[inputValue ]}) + const resp =await kintone.api(kintone.api.url('/v1/users', true), 'GET', {codes: inputValue.join(',')}) + let usersInfo = resp.users; - //入力されたログイン名(メールアドレス)がユーザー情報に登録されている場合、そのユーザー名を取得する - if (resp.users[usersInfoColumnIndex].code === inputValue) { - usersName=resp.users[usersInfoColumnIndex].name; + if (usersInfo.length !== inputValue.length) { + throw new Error(); + } + + //入力されたログイン名がユーザー情報に登録されている場合、そのユーザー名を取得する + for (let indexUsersInfo in usersInfo) { + for(let indexInputUser in inputValue){ + if(usersInfo[indexUsersInfo].code === inputValue[indexInputUser]){ + usersName.push(usersInfo[indexUsersInfo].name); + } + } } //ユーザー名が取得できた場合、ログイン名とユーザー名をフィールドにセットする @@ -225,7 +268,6 @@ export class InsertValueAction implements IAction{ }catch{ return false; } - return usersName; } @@ -291,6 +333,66 @@ export class InsertValueAction implements IAction{ return groupsName; } + /** + * ユーザーオブジェクトを挿入する場合、挿入先フィールドによって、適切なオブジェクトの値を取得し、セットする + * @param {string} inputValue -入力された値 + * @param {string} objectValue -オブジェクト変数 + * @param {string} fieldType -挿入先フィールドタイプ + * @param {string} fieldCode -挿入先フィールドタイプ + * @return {string} -挿入先フィールドによって、ログインユーザーオブジェクトの何の値を返すか、変わる + */ + setValueOfUserObject(inputValue :any,objectValue :any,fieldType : any,fieldCode : any): any{ + + //変数の値 + let variableValue = []; + + //ユーザー選択フィールドに挿入時、ユーザーオブジェクトのcodeを代入する + if(fieldType.includes('USER_SELECT')){ + //変数の値取得 + // variableValue = getContextVarByPath(context.variables,inputValue); + if(!Array.isArray(objectValue)){ + variableValue.push(objectValue.code); + }else{ + for(let i=0;i { + return path.split(".").reduce((o, k) => (o || {})[k], obj); + }; + + //オブジェクト変数で、値の指定がある場合は、変数名のみ取得 + var variablesName = fieldValue.substr(0, fieldValue.indexOf('.')); + if(variablesName){ + //変数の値取得 + objectValue = getContextVarByPath(context.variables,variablesName) + }else{ + //変数の値取得 + objectValue = getContextVarByPath(context.variables,fieldValue) + } + + //オブジェクト変数の場合、プロパティを取得し、何のオブジェクト変数か判断する + if(objectValue !== null && typeof objectValue === 'object'){ + let objectProperties=[] + + if(objectValue.length > 0){ + objectProperties=Object.keys(objectValue[0]); + }else{ + objectProperties = Object.keys(objectValue); + } + //(ログインユーザー・値取得のコンポーネントからの)ユーザーオブジェクトを挿入時、挿入先フィールドによって、値を指定する + if(objectProperties.includes('code') && objectProperties.includes('name')){ + objectValue=this.setValueOfUserObject(fieldValue,objectValue,fieldType,fieldCode); + + //ユーザーオブジェクトから取得した値を、fieldValueArrayに代入 + for (const value of objectValue) { + fieldValueArray.push(value); + } + } + }else{ + //オブジェクト変数でない場合、変数をfieldValueArrayに代入 + variableValue = context.variables[fieldValue]; + fieldValueArray[0] = variableValue; + } + + //変数がfieldValueArrayに代入できなかった場合は、エラーを出す + if(objectValue === undefined && fieldValueArray === undefined){ throw new Error("「"+fieldCode+"」"+"フィールドに入れようとした変数は、無効な入力形式です。"); } - fieldValue = variableValue; + + //変数の値にエラー(空文字・空白文字の混入)がないことをチェックする + notInputError=this.checkVariableValueBlank(fieldType,fieldValueArray,fieldCode,fieldRequired,event); + + //手入力の場合、プロパティ挿入する値をfieldArrayに代入 + }else{ + fieldValueArray.push(fieldValue); + //入力エラー(空白文字の混入)がないことをチェック + notInputError=this.checkInputValueBlank(fieldType,fieldValueArray,fieldCode,fieldRequired,event); } - //入力値チェック後、形式変換、型変換した値を格納する変数 - let correctFormattedValue; - //入力値チェック後、形式変換、型変換した値を格納する配列 - let correctValues :string[] = []; - //入力エラー(空白文字の混入)がないことをチェック - let notInputError=this.checkInputBlank(fieldType,fieldValue,fieldCode,fieldRequired,event); + //形式変換、型変換した値を格納する変数 + let correctFormattedValue = undefined; + //形式変換、型変換した値を格納する配列 + let correctValues :any[] = []; //条件式の結果がtrue、入力エラー(空白文字の混入)がない場合、挿入する値をフィールドタイプ別にチェックする if(conditionResult && notInputError){ //文字列型のフィールドに挿入しようとしている値が適切の場合、correctFormattedValueに代入する if(fieldType === "SINGLE_LINE_TEXT" || fieldType === "MULTI_LINE_TEXT" || fieldType === "RICH_TEXT" || fieldType === "LINK" ){ - correctFormattedValue = fieldValue; + correctFormattedValue = fieldValueArray.join(','); //数値型のフィールドに挿入しようとしている値が適切の場合、数値型に型変換してcorrectFormattedValueに代入する }else if(fieldType === "NUMBER" ){ - if(this.checkInputNumber(fieldValue,fieldCode,event)){//入力値チェック - correctFormattedValue = Number(fieldValue);//型変換 + if(this.checkInputNumber(fieldValueArray[0],fieldCode,event)){//入力値チェック + correctFormattedValue = Number(fieldValueArray[0]);//型変換 } //日付・日時型のフィールドに挿入しようとしている値が適切の場合、指定の日付・日時に形式変換してcorrectFormattedValueに代入する }else if(fieldType === "DATE" || fieldType === "DATETIME" ){ - if(this.checkInputDate(fieldValue,fieldCode,event)){//入力値チェック - let formattedDate = this.changeDateFormat(fieldValue,fieldType,fieldCode,event) + if(this.checkInputDate(fieldValueArray[0],fieldCode,event)){//入力値チェック + let formattedDate = this.changeDateFormat(fieldValueArray[0],fieldType,fieldCode,event) if(formattedDate){ correctFormattedValue = formattedDate } @@ -403,33 +554,35 @@ export class InsertValueAction implements IAction{ //時刻フィールドに挿入しようとしている値が適切の場合、correctFormattedValueに代入する }else if(fieldType === "TIME"){ - if(this.checkInputTime(fieldValue,fieldCode,event)){//入力値チェック - correctFormattedValue = fieldValue; + if(this.checkInputTime(fieldValueArray[0],fieldCode,event)){//入力値チェック + correctFormattedValue = fieldValueArray[0]; } //ラジオボタン・ドロップダウンのフィールドの選択肢と入力値が一致した場合、correctFormattedValueに代入する }else if(fieldType === "RADIO_BUTTON" || fieldType === "DROP_DOWN"){ - if(this.checkInputOption(fieldValue,fieldOptions,fieldCode,event)){//入力値チェック - correctFormattedValue = fieldValue; + if(this.checkInputOption(fieldValueArray[0],fieldOptions,fieldCode,event)){//入力値チェック + correctFormattedValue = fieldValueArray[0]; } //チェックボックス・複数選択のフィールドの選択肢と入力値が一致した場合、correctValuesの配列に代入する }else if(fieldType === "CHECK_BOX" || fieldType === "MULTI_SELECT" ){ - if(this.checkInputOption(fieldValue,fieldOptions,fieldCode,event)){//入力値チェック - correctValues[0] = fieldValue; + if(this.checkInputOption(fieldValueArray[0],fieldOptions,fieldCode,event)){//入力値チェック + correctValues[0] = fieldValueArray[0]; } - //ユーザー情報フィードに挿入しようとした値が適切な場合、correctFormattedValueに代入する + //ユーザー情報フィードに挿入しようとした値が適切な場合、correctValues(配列)に代入する }else if(fieldType === "USER_SELECT"){ //挿入する値がユーザー情報から見つかれば、ユーザー名を格納 - let users=await this.setInputUser(fieldValue); + let usersName=await this.setInputUser(fieldValueArray); - //ユーザー名が格納できている場合、ログイン名とユーザー名をcorrectFormattedValueに代入する - if(!users){ + //ユーザー名が格納できている場合、ログイン名とユーザー名をcorrectValues(配列)に代入する + if(!usersName){ event.record[fieldCode]['error']="ユーザー選択に、挿入しようとしたユーザー情報は見つかりませんでした。「値を挿入する」コンポーネントの処理を中断しました。"; throw new Error("ユーザー選択に、挿入しようとしたユーザー情報は見つかりませんでした。「値を挿入する」コンポーネントの処理を中断しました。"); - }else{ - correctFormattedValue=[{ code: fieldValue, name: users }]; + } + + for(let indexInputUser in fieldValueArray){ + correctValues.push({ code: fieldValueArray[indexInputUser], name: usersName[indexInputUser] }); } //組織情報フィードに挿入しようとした値が適切な場合、correctFormattedValueに代入する @@ -464,12 +617,12 @@ export class InsertValueAction implements IAction{ if(!event.type.includes('success')){ //条件式の結果がtrueかつ挿入する値が変換できた場合、フィールド(ラジオボタン・ドロップダウン・チェックボックス・複数選択・文字列一行・文字列複数行・リッチエディタ・数値・日付・日時・時刻)にセット - if(conditionResult && (correctFormattedValue || correctValues)){ + if(conditionResult){ //条件式の結果がtureかつ、値を正しい形式に変換できた場合、フィールドに値をセットする - if(correctFormattedValue){ + if(correctFormattedValue !== null && correctFormattedValue !== undefined){ event.record[fieldCode].value = correctFormattedValue; //条件式の結果がtureかつ、値を正しい形式(配列)に変換できた場合、フィールドに値(配列)をセットする - }else if(correctValues.length > 0){ + }else if(correctValues.length > 0 && correctValues !== null && correctValues !== undefined){ event.record[fieldCode].value = correctValues; } }