O
O
OpenReligion2019-02-25 13:14:39
Google Apps Script
OpenReligion, 2019-02-25 13:14:39

How to replace a library with a web application in Google Script?

At the moment, the following code is inserted as a script in many tables:

function append()
{
  SpreadsheetApp.getUi().alert('Всё сохранилось! Спасибо!');
  Library.called_by_button();
}

The entire executable part is moved to the Library plug-in library.
There is a problem. If you make changes to the Library, then in each individual file you need to manually change the version of the included file to the latest one.
As far as I understand, if you deploy the same Library as a web application (UrlFetchApp.fetch), then it will be enough just to publish (update) the web application file, and in all files it will automatically become relevant.
The library now takes values ​​from an open table and sends them to another one deployed as a web application. The library code now looks like this:
function called_by_button(data_to_be_sent){

var date = new Date();  
var ss1 = SpreadsheetApp.getActive();
var id = ss1.getRange("B1").getValue();

  var data = {
            'date': date,
            'a1': id

  var options = {
    'method' : 'post',
    'payload' : data,
    muteHttpExceptions: true
  };
  // This is probably the best way to use UrlFetchApp() and handle errors.
  
  var url = 'https://script.google.com/a/***/exec';
  
  try {
    var response = UrlFetchApp.fetch(url, options); // Post the data (make the HTTP Request)
    var responseCode = response.getResponseCode();
    if (responseCode === 200) { // 200 = SUCCESS
      Logger.log("url_fetch() response code %s ", responseCode);
      return response; 
    } else {
      Logger.log(Utilities.formatString("url_fetch() Request failed for: %s, Expected 200, got %d",url,responseCode ));
      return false; 
      // 
    }
  }// end Try
  catch (err) {
     Logger.log(Utilities.formatString("url_fetch() Request failed (underlying network error). %s, response code: %s",err, responseCode));
     return false;
  }

  }

Can you please tell me what should be changed in the code itself in both files (in the open table and in the library) in order for the library to work as a web application?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2019-02-26
@oshliaer

You probably don't need to change anything in your code. We need to try to change our understanding of the problem. This is not a desktop office, where I threw a couple of symlinks, and the program is happy.
You must change the approach to solving the problem. One of the best options is to use a web application as a central program. But it has its limitations, for example, to send an alert to some Table, you have to puff. But if the user clicks on a button in the web application itself, then you can issue an alert on his side.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question