1

So, I got a google apps script, which is placed on my spreadsheet 'AllScripts'. I'm not the one who wants it like that, but it must be written in a separated spreadsheet.

On the spreadsheet 'wb1' apps scripts I'm importing the code from 'AllScripts' as a library, so I can call its methods on 'wb1'. Still on 'wb1', there is the onOpen method, which creates a menu and add an item with a method from 'AllScripts'. All good 'till here.

The minimal reproducible code is available on below spreadsheets.

These are the 'wb1' spreadsheet and 'AllScripts' spreadsheet, respectively: https://docs.google.com/spreadsheets/d/1uQz8q1AE7vs9eJCajnIAPmQZRQgBoZv1mF9dcH9D_l8/edit?usp=sharing https://docs.google.com/spreadsheets/d/1YaO18yRagzG4wA46AGnzn94CR6Vy6ENZcsbLZKN6OgM/edit?usp=sharing

Note that at first you won't see the menu and can't run any code, so on 'wb1' go to Extensions > Apps Script and then run the onOpen. It will ask you for permissions, and I don't know why, it will open a window with a security warning.

Anyways, when you open the modal from the menu Scripts > Test and write the asked stuffs, open your console on the browser and you should see something like this: [![error][1]][1]

I don't know how to read that, but apparently the code from modal opened on 'wb1' cannot open a method from 'AllScripts'.

How can we overcome this?

These are some code from the script:

ShowForm.gs:

function showForm() {
  const form = HtmlService.createTemplateFromFile('Form').evaluate().setHeight(480);
  SpreadsheetApp.getUi().showModalDialog(form, 'Request Data Window');
}

SaveData.gs:

function saveData(data) {
  const iasRequestsSpreadsheet = SpreadsheetApp.openById('1uQz8q1AE7vs9eJCajnIAPmQZRQgBoZv1mF9dcH9D_l8');
  const sheet = iasRequestsSpreadsheet.getSheetByName('page1');

  const line = getFirstEmptyRow(sheet);

  // request date
  sheet.getRange(line, 1).setValue(new Date());

  // requested by
  sheet.getRange(line, 2).setValue(data.requestedBy);

  const currentWorkbook = SpreadsheetApp.getActiveSpreadsheet()
    .getName()
    .split('-')
    .trim();
  // product
  sheet.getRange(line, 3).setValue(currentWorkbook);

  const tabName = SpreadsheetApp.getActiveSheet().getName();
  // activity
  sheet.getRange(line, 4).setValue(`Do stuffs on ${tabName}`);

  // due date
  sheet.getRange(line, 6).setValue(data.dueDate);

  // status
  sheet.getRange(line, 8).setValue('Pending');

  // comments
  sheet.getRange(line, 9).setValue(data.comments);
}

Form.html: ps.: I removed the Materialize stuffs, so it can be easier to read as suggested by @Rubén.

<!DOCTYPE html>
  <html>
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>

    <body>
        <form>
            <label for="requestedBy">Requested By</label>
            <input id="requestedBy" type="text" placeholder="John Doe">

            <label for="dueDate">Due Date</label>
            <input id="dueDate" class="datepicker">

            <label for="comments">Comments</label>
            <textarea id="comments" placeholder="Please ignore lines X, Y and Z..." class="materialize-textarea"></textarea>
            <button id="btn">
              Submit
            </button>
        </form>

      <script>
        document.getElementById('btn').addEventListener('click', getData);

        function getData() {
          var requestedBy = document.getElementById('requestedBy').value;
          var dueDate = document.getElementById('dueDate').value;
          var comments = document.getElementById('comments').value;

          var data = {
            requestedBy: requestedBy,
            dueDate: dueDate,
            comments: comments
          };
          
          google.script.run.saveData(data);
        }
      </script>
    </body>
  </html>
João Casarin
  • 674
  • 2
  • 9
  • 27
  • Please read https://developers.google.com/apps-script/guides/html/communication, more specifically https://developers.google.com/apps-script/guides/html/communication#forms – Rubén Feb 07 '22 at 02:59
  • If you need further help, replace the code in the question by a [mcve] related exclusively to what you need help with. – Rubén Feb 07 '22 at 03:03
  • How do you open your HTML of `Form.html`? – Tanaike Feb 07 '22 at 03:17
  • Hey @Rubén, here is a 'working' example (actually it doesn't work, right? hahaha): https://docs.google.com/spreadsheets/d/1uQz8q1AE7vs9eJCajnIAPmQZRQgBoZv1mF9dcH9D_l8/edit#gid=0 – João Casarin Feb 07 '22 at 04:40
  • Hey @Tanaike oh I'm sorry, I did not notice that piece of code is missing. Already updated the post with it and an reproducible spreadsheet. – João Casarin Feb 07 '22 at 04:42
  • Honestly, I have just tested the reproducible code, and it is working, but it doesn't on my main code... There are some sensitive information there, so I can't actually share that spreadsheet – João Casarin Feb 07 '22 at 04:48
  • Thank you for replying. I could see the method for opening your HTML. I have a question. I cannot understand `With the script finished, I can't handle the data from the modal.`. Can I ask you about the detail of it? By the way, unfortunately, I cannot open your provided sample Spreadsheet. Could you please confirm it again? – Tanaike Feb 07 '22 at 05:01
  • Can you try the link again? So, about that phrase, I thought I was not able to retrieve the data from the form and pass it to some functions, but apparently with that reproducible link I can, but it still doesn't work. Like, it was supposed to append a row to a specific sheet in a workbook when I click on 'submit', but after that, the form just closes and nothing happens to the sheet. – João Casarin Feb 07 '22 at 05:47
  • Thanks for your reply JoãoCasarin. The spreadsheet is not shared, anyway by [mcve] I meant that you should remove all the codelines that are not necessary to pass a minimal representation of your form data to the spreadsheet. i.e. the Materialize libraries are not necessary, only a single input tag is enough, the getSelectedExamples function is not necessary. – Rubén Feb 07 '22 at 05:49
  • Hey @Rubén , I have just updated the whole question with new details and explaining a little more. Also, I think this is the closest situation I can get to without sharing sensitive data. Please tell me if anything else is required! – João Casarin Feb 08 '22 at 03:32
  • hey @Tanaike the question is now updated and it is apparently better to understand and reproduce the error. – João Casarin Feb 08 '22 at 03:33
  • Thanks for your reply JoãoCasarin. Basically what you are trying, calling a library function directly from client-side code is not possible. Please checkout the answers to the associated questions. – Rubén Feb 08 '22 at 04:05
  • @Rubén I asked to reopen the question, since I tested those linked questions but did not work as expected. Could you please look at the EDIT part of the question? – João Casarin Feb 08 '22 at 04:46
  • @JoãoCasarin The question has become a real mess, I suggest you to revert the last edit and post a new question. – Rubén Feb 08 '22 at 04:54
  • By the way, in the new question please include the appsscript.json files, and try to reduce even more the code lines (i.e. there is no need to include so many `sheet.getRange(...).setValue(...)` including one is enough) – Rubén Feb 08 '22 at 04:59

1 Answers1

4

Try

in html

  function getData() {
    var data = document.forms[0]
    var tab = []
    for (var i=0;i<data.length;i++){
    if (data[i].type != "button"){
        tab.push(data[i].value)
      }
    }
    google.script.run.saveData(tab);
    // document.forms[0].reset()
  }

in gs

function saveData(tab) {
  const iasRequestsSpreadsheet = SpreadsheetApp.openById('exampleid');
  const sheetName = iasRequestsSpreadsheet.getSheetByName('examplename');
  sheetName.getRange(1, 1,1,tab.length).setValues([tab]);
}
Mike Steelson
  • 14,650
  • 2
  • 5
  • 20
  • Hey Mike, thanks for the reply, but this didn't solve... Nothing different happened unfortunately – João Casarin Feb 07 '22 at 17:46
  • This is a generic solution I use with my forms ... so, as Rubén asked you, give us a minimal reproducible example – Mike Steelson Feb 07 '22 at 18:20
  • @João Casarin I implemented my solution and it works like a charm! – Mike Steelson Feb 08 '22 at 01:20
  • Hey @Mike Steelson, I have just updated the question. I think your code should happen if I was using a single spreadsheet, but as you can see in the new details, it is separated into multiple spreadsheets, can you take a look at this? – João Casarin Feb 08 '22 at 03:34
  • No, this post is now closed. I answered your previous question and it works. So accept this answer for others to enjoy. I spent some time to do it. Sorry for the development of the question but it is now closed. Post a new one. – Mike Steelson Feb 08 '22 at 05:53