Что делает программа. Я написал простой скрипт Google Apps, который извлекает непрочитанные сообщения электронной почты с ярлыка в учетной записи Gmail, в которую вы вошли в систему, и возвращает количество этих писем и отображает его на веб-сайте.

Проблема: эта программа работает, когда я создал веб-приложение на компьютере, на котором оно было разработано, и запустил его с веб-сайта. Однако, если я скопирую URL-адрес и отправлю его на другой компьютер, который вошел в систему как другая учетная запись Google, это не сработает.

Код .

/*Sample.gs*/ 
function doGet(e){
    return HtmlService.createHtmlOutputFromFile('Run.html');
}

function GetUnreadCount(sourceLabel){
    return GmailApp.getUserLabelByName(sourceLabel).getUnreadCount();
}

function Main()
{   
    /*For testing, return unread email count from "CustomerA" label of Gmail*/
    return GetUnreadCount("CustomerA");
}

.

/*Run.html*/
<!DOCTYPE html> 
<html>
  <head>
    <base target="_top">
    <script>
        function CallGetUnreadCount() {

          document.getElementById('Messages').innerHTML = 'Counting...';

          google.script.run
            .withSuccessHandler(onSuccess)
            .withFailureHandler(onFailure)
            .Main();          
        }

        function onSuccess(UnreadCount) 
        {
          document.getElementById('Messages').innerHTML = "There are " + UnreadCount + " unread emails.";
        }

        function onFailure(error) 
        {
          document.getElementById('Messages').innerHTML = "Err: " + error.message;
        }

    </script>
  </head>
  <body>
    <div>
        <h1 id="Messages">Count # of unread emails</h1>
    </div>
    <div>
        <button type="button" onclick='CallGetUnreadCount();' id="CountButton">Count Unread Emails</button> 
    </div>
  </body>
</html>

Эта программа отлично работает, если я запустил ее на компьютере, на котором написал этот сценарий. Однако, если я развертываю его как веб-приложение, распространяю URL-адрес (заканчивающийся на /exec) и запускаю его на другом компьютере, который вошел в систему под другой учетной записью Gmail, он всегда возвращает 0, даже если есть непрочитанные электронные письма с ярлыком CustomerA. Меня смущает то, что эта же программа работает на другом компьютере, если я создаю новый проект на этом компьютере, копирую и вставляю приведенный выше код в файл .gs и файл html и развертываю его как Интернет. приложение.

Почему одна и та же программа работает, если каждый компьютер развертывает веб-приложение самостоятельно, но не работает, если я распространяю URL-адрес?

1
J.Doe 7 Ноя 2019 в 02:16
1
Can you provide the information about the settings of "Deploy as web app" and "Execute the app as" for deploying your Web Apps? And I cannot understand about each computer of if each computer deploys the web app on their own. each computer means each Google account?
 – 
Tanaike
7 Ноя 2019 в 02:23
@Tanaike-san, 以前もお世話になりました。再びありがとうございます。まず、コンピュータAでこのプログラムを書き、スクリプト画面のPublishを選択しDeploy As Web Appを選択しました。そこで、Web AppのURLは取得でき、そのWebサイトからこのプログラムを実行すると、ちゃんと未読メールの件数を取得できます。このコンピュータは、仮にAさんのGoogleアカウントでログインしており、AさんのGmailには、CustomerAというラベルがあります。そこの未読件数を数えます。  そして、このDeployされたWebAppのURLを別のコンピュータBにメールか何かで送ります。このコンピュータBは、Bさんがログインしていますが、BさんのGmailにもCustomerAというラベルがあります。同じWeb AppのURLを使って、Bさんも自身のCustomerAラベルから未読件数を数えたいのです。しかし、なぜかこれができず、いつも「0」が返ってきます。しかし、コンピュータAから、このGSファイルとHTMLファイルをメール等でコンピュータBへ送り、新プロジェクトを作成し、これらのコードを同じようにコピーし、そこからDeploy As Web Appをすると、コンピュータBでもWebAppがちゃんと動きます。同じコードなのに不思議です。
 – 
J.Doe
7 Ноя 2019 в 02:28
1
Thank you for replying. From your replying, if you are using the script editor with Japanese language, when you select "ウェブアプリケーションとして導入" and open a dialog, what are the values of Execute the app as: and Who has access to the app:? I thought that your issue might be resolved by the settings of Web Apps.
 – 
Tanaike
7 Ноя 2019 в 02:37
1
Who has access to the app はAnyoneにしていましたが、 Execute the app as のところは、コンピュータAのアカウントになっていました^^;そこを設定しなおすと直りました。こんな単純なことに何時間も悩んでいました。ありがとうございます!
 – 
J.Doe
7 Ноя 2019 в 02:44
1
Thank you for replying. I'm glad your issue was resolved. 無事解決したとのことで安心しました。 For example, about the settings of Web Apps, is this information useful? github.com/tanaikech/… When your issue was resolved, can you post it as an answer? By this, it will be useful for other users who have the same issue.
 – 
Tanaike
7 Ноя 2019 в 02:47

1 ответ

Лучший ответ

When Deploying as web app (Publish > Deploy as web app), there are a few options to configure:

deploy as web app

  • Execute the app as: Can be either Me, or User accessing the web app. For your case, you must use the later to make it work as you expect.
  • Who has access to this app: Can be either Only myselft, Anyone within your domain or Anyone. You have to make sure it's either the 2nd or 3rd option, depending on your needs, so that the user can properly execute it.
1
carlesgg97 7 Ноя 2019 в 12:55