2017-08-04 7 views
0

私はお詫び申し上げます。私はこれが新人質問で、簡単な答えであると確信しています。電子メール通知スクリプトで "null"フィールドのエラー

したがって、ユーザーがエントリを作成したときに電子メール通知を送信するには、次の方法を使用します。 "null"(空白)のエントリに関連する2つの動作を除いて、すべて正常に動作します。

function newSalesEmailMessage(sendButton) { 
    var pageWidgets = sendButton.root.descendants; 
    var fullName = app.datasources.CurrentUser.item.FullName; 
    var htmlbody = '<b><font size="3">' + fullName + '</font></b>' + ' has created a new sales entry for: ' + 
     '<h1><span style="color:#2196F3">' + pageWidgets.ShowName.value + '</h1>' + 
     '<p>Shoot Date: <b>' + pageWidgets.ProjectDate.value.toDateString() + '</b>' + 
     '<p>Contact: <b>' + pageWidgets.Contact.value + '</b>' +  
     '<p>Post AP: <b>' + pageWidgets.PostAP.value + '</b>' + 
     '<p>Sales Person: <b>' + pageWidgets.SalesPerson.value + '</b>' + 
     '<p>Notes: <b>' + pageWidgets.Notes.value + '</b>'; 



    google.script.run 
    .withSuccessHandler(function() { 
    }) 
    .withFailureHandler(function(err) { 
     console.error(JSON.stringify(err)); 
    }) 
    .sendEmailCreate(
     '[email protected]', 
     'New Sales Entry for: ' + pageWidgets.ShowName.value, 
     htmlbody); 
    return sendButton === "" || sendButton === null || sendButton === undefined; 
} 

問題1:ユーザーがフィールドを空白のままにするたびにメールは理にかなっている、フィールドに「ヌル」を置きますが、人々は私を求め続ける「ナルは誰?」ああ。

"null"の代わりに空白(例: "")を出力する方法があれば、それは素晴らしいでしょう。

問題2:ユーザーがProjectDate(日付フィールド)を空白にすると、次のエラーが表示されます。nullのプロパティ 'toDateString'を読み取ることができません。

これは意味がありますが、明らかにこれによりこのスクリプトは完了しません。このようなもので両方の問題を解決できると期待していましたが、そうではありませんでした。

function newSalesEmailMessage(sendButton) { 
    var pageWidgets = sendButton.root.descendants; 
    if (pageWidgets === null) { 
    pageWidgets = ""; 
    } 
    var fullName = app.datasources.CurrentUser.item.FullName; 
    var htmlbody = '<b><font size="3">' + fullName + '</font></b>' + ' has created a new sales entry for: ' + 
     '<h1><span style="color:#2196F3">' + pageWidgets.ShowName.value + '</h1>' + 
     '<p>Shoot Date: <b>' + pageWidgets.ProjectDate.value.toDateString() + '</b>' + 
     '<p>Contact: <b>' + pageWidgets.Contact.value + '</b>' +  
     '<p>Post AP: <b>' + pageWidgets.PostAP.value + '</b>' + 
     '<p>Sales Person: <b>' + pageWidgets.SalesPerson.value + '</b>' + 
     '<p>Notes: <b>' + pageWidgets.Notes.value + '</b>'; 



    google.script.run 
    .withSuccessHandler(function() { 
    }) 
    .withFailureHandler(function(err) { 
     console.error(JSON.stringify(err)); 
    }) 
    .sendEmailCreate(
     '[email protected]', 
     'New Sales Entry for: ' + pageWidgets.ShowName.value, 
     htmlbody); 
} 

ありがとうございました。

答えて

2

、このような状況を処理するために複数の方法がありますが、最も簡単な1は、いくつかのヘルパー関数を導入します:

// This function will return empty string in case widget's value is null 
// You can return any default value or even pass it as function's parameter 
function getSafeString(widget) { 
    return widget.value === null ? 
     '' : 
     widget.value; 
} 

function getSafeDate(widget) { 
    return widget.value === null ? 
     '' : 
     widget.value.toDateString(); 
} 

function newSalesEmailMessage(sendButton) { 
... 
'...some HTML...' + getSafeString(widgets.SomeWidget) + '...some HTML...' + 
'...some HTML...' + getSafeDate(widgets.DateWidget) + '...some HTML...' 
... 
} 

ちなみに、私はまたdatasource.item(またはdatasource.modes.createを使用してお勧めします。あなたのバインディングに応じてアイテム)を使用して、ユーザー入力を取得します。

+0

これは機能しました。どうもありがとうございました。 –

関連する問題