Google apps script - drawing as button to open Google form

老子叫甜甜 提交于 2020-12-16 07:10:01

问题


So I work with a very small business and I am working on shared contact database in Google Sheets. One of the things I would like to include is a simple button (I just used a drawing I made in Sheets) that says "Add New Contact" and when clicked it opens up a Google Form I created that will collect the contact info and store in it the Google Sheet.

My question is... What Google app script can I use to achieve that task? I'm very new to Google Apps script so I don't really know how to go about writing the code properly.

Thanks for your answers!


回答1:


I just want a Simple Button!

The easiest way to create a button is to create a new html file in the script editor. When you do, it will open up a file and ask for a name. You add the name "AddNewContent" and look in the file and it will have this content.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

  </body>
</html>

Add content so that it looks like this:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function addNewContent()
      {
        google.script.run.addNewContent();
      }
    </script>
  </head>
  <body>
    <input id="btn0" type="button" value="Add New Contact" onClick="addNewContact();" />
  </body>
</html>

Now go to the Code.gs file. When you start a new project the Code.gs file is added automatically and it contains the following:

function myFunction(){

}

Replace the code with this:

function addNewContent()
{

}

Now you have a button that's connected to the shell of a Google Apps Script Function. To display the button on a sidebar you can create another function in the Code.gs file like the one below:

function showSideBar()
{
  var userInterface=HtmlService.createHtmlOutputFromFile('AddNewContent');
  SpreadsheetApp.getUi().showSidebar(userInterface);
}

And then to display the sidebar easily you can add the showSideBar function to a menu which will be created whenever you open your spreadsheet.

function onOpen()
{
  SpreadsheetApp.getUi().createMenu('My New Menu').addItem('Display my Sidebar', 'showSideBar').addToUi();

}

So if you save all of those files and select the onOpen function in the code editor so that the menu looks like the image below and press the triangular button then it will run the onOpen function and you won't have to close and reopen the program to get it to run and the menu will magically appear on the spreadsheet. Now you can display your side bar and run your Google Apps Script function.

Unfortunately, the function doesn't do anything yet because I don't actually know what you want to do or have down. If you already created a Google Form then it has already linked itself to a spreadsheet and it's already collecting data. If you have created an HTML form of your own (which is doubtful) then you can display it as a dialog or deploy it as a web app.

So the real question is what have you done and where do you want to go from here. Unfortunately, writing Google Apps Script requires a fair knowledge of Javascript, HTML, a little CSS and in my case a bit of JQuery. It also involves a far amount of reading and research. And while you may not believe it now. Most of the questions I had about Google Apps Script are found in the documentation. Which is where I'd recommend that you go and read and then reread again and again until you know where to find everything you need to write your scripts. Most of the time when I need something I can find it in a Google Search and very often it will bring me right back to this site. But many of the answers to questions involve a fair amount of learning on my part.

The first time I tried to learn Google Apps Script was in 2009 and I finally gave up because I just got lost looking for documentation. But about 10 months ago I took another look and really liked what I saw in terms of how the documentation was organized so I jumped in again. A couple of months later I started visiting StackOverFlow.com and then I answered a few questions here and there and now about 8 months later I feel pretty good about many spreadsheet related tasks. I'm still weak in several areas and hanging around this site helps to remind me of all the things I could be learning.



来源:https://stackoverflow.com/questions/45734375/google-apps-script-drawing-as-button-to-open-google-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!