简体   繁体   English

如何在不反复设置触发器和身份验证的情况下重用 App 脚本?

[英]How can I reuse an App Script without setting up triggers and auth over and over?

Update: I'm sorry I didn't post the code originally.更新:对不起,我最初没有发布代码。 In my newbie-ness, I didn't think it honestly mattered, but I've been around long enough to know better.以我的新手身份,我认为这真的不重要,但我已经待了足够长的时间才能知道得更多。 See below:见下文:

function openForm(e)
{
  populateQuestions();
}

function populateQuestions() {
  var form = FormApp.getActiveForm();
  var googleSheetsQuestions = getQuestionValues();
  var itemsArray = form.getItems();
  itemsArray.forEach(function(item){
    googleSheetsQuestions[0].forEach(function(header_value, header_index) {
      if(header_value == item.getTitle())
      {
        var choiceArray = [];
        for(j = 1; j < googleSheetsQuestions.length; j++)
        {
          (googleSheetsQuestions[j][header_index] != '') ? choiceArray.push(googleSheetsQuestions[j][header_index]) : null;
        }
        item.asCheckboxItem().setChoiceValues(choiceArray);
        // If using Dropdown Questions use line below instead of line above.
        //item.asListItem().setChoiceValues(choiceArray);
      }
    });     
  });
}

function getQuestionValues() {
  var ss= SpreadsheetApp.openById('1QeckPxMYSYGMkZ-QggY76u03N1qBKBGL2UEMcUvu7sM');
  var questionSheet = ss.getSheetByName('Sheet5');
  var returnData = questionSheet.getDataRange().getValues();
  return returnData;
}

After months of work, I finally have cracked the script to getting what I wanted.经过几个月的工作,我终于破解了脚本以获得我想要的东西。 Now I need to use this script over and over in multiple Google Forms (forms, for short).现在我需要在多个 Google Forms(简称表单)中反复使用这个脚本。 By copy and pasting the Form within Drive, I carry the App Script with each copy, but not the trigger or the authorization needed to run it.通过在 Drive 中复制和粘贴表单,我在每个副本中都携带了 App 脚本,但没有运行它所需的触发器或授权。 I need assistance in learning how I can make this process quicker.我需要帮助来学习如何使这个过程更快。

I have very limited knowledge of add-ons and things, but I'm willing to learn.我对附加组件和事物的了解非常有限,但我愿意学习。 I do not necessarily need this to be a public access Script, just want to make my life less repetitious.我不一定需要这是一个公共访问脚本,只是想让我的生活不那么重复。

I would also like to make it so after installing the add-on, users can just pick the Sheet/Tab through a UI and not editing the code specifically, but that's a different story.我还想在安装附加组件后做到这一点,用户只需通过 UI 选择工作表/选项卡,而不是专门编辑代码,但这是另一回事。

For the moment, You can create an installable trigger so you don't have to add the trigger manually anymore.目前,您可以创建一个可安装的触发器,这样您就不必再手动添加触发器了。 You should use this:你应该使用这个:

function createFormOpenTrigger() {
  const gf = FormApp.getActiveForm()
  ScriptApp.newTrigger('populateQuestions')
      .forForm(gf)
      .onOpen()
      .create();
}

Instead of:代替:

function openForm(e)
{
  populateQuestions();
}

As for the credentials, deploying the script as an add-on I will help you with the OAuth part.至于凭据,将脚本部署为附加组件,我将帮助您完成 OAuth 部分。 However, I was unable to test it today due to propagation.但是,由于传播,我今天无法对其进行测试。 I'm still getting a message that the Apps Script is not bound to the GCP project.我仍然收到 Apps 脚本未绑定到 GCP 项目的消息。 I will try my luck tomorrow, in the meantime you can read the following documentation about it.明天我会试试运气,同时你可以阅读以下关于它的文档。 Here . 在这里


Sorry, I took some time to post the answer since you mentioned that you didn't know how to create an add-on, so I tried to add step by step information.抱歉,由于您提到您不知道如何创建附加组件,我花了一些时间来发布答案,所以我尝试逐步添加信息。


Update:更新:

You can add this part at the top of the code, this will create a menu that will allow you to run the script for the first time in the Google Form after we make it an Add-on.您可以在代码顶部添加此部分,这将创建一个菜单,在我们将其设为附加组件后,您可以在该菜单中首次在 Google 表单中运行该脚本。

function onOpen() {
  //you can change the names of the menu, just not change
  // this part "createFormOpenTrigger"
  FormApp.getUi()
    .createMenu('formTest')
    .addItem('Run Apps Script', 'createFormOpenTrigger')
    .addToUi();
}

If you do not have a GCP project, you can created one by following the steps here , or following the steps:如果您没有 GCP 项目,您可以按照此处的步骤或以下步骤创建一个:

  1. Open the Google API Console projects list .打开 Google API控制台项目列表
  2. Click Create Project .单击创建项目
  3. Fill out the project information for your add-on.填写附加组件的项目信息。
  4. Click Create .单击创建

Copy the Project number , and add it to the Apps Script to switch to a different standard Cloud project .复制项目编号,并将其添加到 Apps 脚本以切换到不同的标准云项目

  1. 第一步 添加项目
  2. 第二步添加项目

Deploy the Apps Script as an Add-on.将 Apps 脚本部署为附加组件。

  1. 步骤 1 添加部署附加组件
  2. step 2 添加部署插件
  3. step 3 添加部署插件

Remember the version number you will need later on.请记住您稍后需要的版本号。 Access to the Google Cloud Project link with the Apps Script.使用 Apps 脚本访问 Google Cloud 项目链接。

Setup the OAuth consent screen , select internal to make sure that only you have access to it (and users of your Workspace if you have one)设置OAuth 同意屏幕,select internal以确保只有您有权访问它(以及您的工作区的用户,如果有的话)

OAuth 同意屏幕

We will need to the Add-on listing , so we will need to add "Google Workspace Marketplace SDK" in the Library .我们将需要加载项列表,因此我们需要在Library中添加“Google Workspace Marketplace SDK”。

  1. 步骤 1 添加库
  2. 第二步添加库
  3. 步骤 3 添加库

Add the App configuration information, add all the require information there, the only recommendation I can provide you are these 2:添加应用程序配置信息,在那里添加所有需要的信息,我可以为您提供的唯一建议是这些 2:

  1. 建议 1

  2. 建议 2

    Note: this can be found under "Project Settings"注意:这可以在“项目设置”下找到项目设置

Lastly, fill out the information under "Store listing".最后,填写“商店列表”下的信息。

商品详情

Note: You can add dummy information if only you are going to use the add-on.注意:如果您只打算使用附加组件,则可以添加虚拟信息。

Once all that is done, use the "App URL" link to install the add-on.完成所有这些后,使用“App URL”链接安装附加组件。 When you open a Google Form it will show up under the Add-on list:当您打开 Google 表单时,它将显示在加载项列表下:

附加清单

And you will see the option to Run the Apps Script, thanks to the last part of the code we added.由于我们添加的代码的最后一部分,您将看到运行 Apps 脚本的选项。

运行脚本

Click on it, and it might ask you for permission the very first time you use it.单击它,它可能会在您第一次使用它时询问您的许可。 But it will not request it again even if you add it to a new form.但是即使您将其添加到新表单中,它也不会再次请求它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 作为对整个实例没有管理员权限的用户,如何为 GitLab 上的所有新项目设置默认分支名称? - How can I set the default branch name for all new projects on GitLab, as an user without admin right over entire instance? 如何从 Firestore 检索 map 并对其进行迭代? - How can I retrieve a map from Firestore and iterate over it? Azure 逻辑应用程序 - 如何迭代 json? - Azure logic app - how to iterate over json? 我无法在 addOnSuccessListener 中迭代 UploadTask - I can't iterate over UploadTask in addOnSuccessListener 我们可以在 Firebase 身份验证触发器(即 onCreate)上访问请求 header 吗? - Can we access request header on Firebase Auth triggers i.e onCreate? 如何在多个星期的两个日期之间使用 CASE 语句创建自定义的“一年中的一周”? - How can I create a custom 'Week of Year' using a CASE Statement BETWEEN two dates over multiple weeks? 如何在 Application Insights 中显示超过 RabbitMQ 的端到端事务? - How can I show a end-to-end transaction over RabbitMQ in Application Insights? 我如何迭代 boto3 中的嵌套字典和列表以获得特定值? - How can I itterate over nested dictionaries and lists in boto3 to obtain particular values? 我可以通过 Firebase 授权用户并使用 Supabase(私有)存储桶而不依赖 Supabase Auth 用户限制吗 - Can I Auth a user through Firebase and use Supabase (private) buckets without relying on Supabase Auth users limitation 如何在Flutter为我的Android App设置App Check? - How can I set up App Check for my Android App in Flutter?
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM