I have google spreadsheet with script and sidebar (html input fields). I will share this spreadsheet with my colleagues. and my colleagues will make a copy of my spreadsheet for personal use.
this is problem
when I edit my script, I want to edit my colleagues's spreadsheet script that copies of my spreadsheet.
for this, Here is my idea.
when my colleague run specific button(scirpt) on sheet first of all before use sheet, automatically I get edit permission of that spreadsheet. and then I go to my colleagues's spreadsheet script and edit it. Is their any script for this?
one script file and run multiple spreadsheets. my script is for input some data specific sheet and cell by html input form on sidebar. There are several input forms and specific sheet names and cell addresses are specified to set values. Is their any way for 2? I've tried publish add-on. but didn't work well. plz give me some idea.
For the first part, you can just add the lines;
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addEditor('----#-------');
To directly create / edit script files you'll have to use the App Script API
App Script API
For the second part, you can write the scripts in a stand-alone file and use the script library in the spreadsheet. Leaving the script in development mode will always use the current code in the script file.
Apps-script libraries
Related
I'm new to GAS and JavaScript in general, so I'd like some help adapting a script from a spreadsheet to a Web App.
Based on some scripts I found, I developed a code to work the way I need in a Google Spreadsheet, but after making it work exactly the way I need it, I realized that a Web App could be a better alternative, mainly because of how it works on Mobile .
The point is that I didn't have a very linear JavaScript learning curve, my learning was solving specific needs, so I have difficulty with some basic concepts... and to be quite honest, deeply understanding JavaScript is not my main focus, but this knowledge is missing me now...
Let's get straight to the point
My current spreadsheet is this one:
Google Sheet - Stack Demonstration
In the GAS linked to this worksheet there are 2 .gs files and one HTML.
GAS files
1 - CSV.gs | Contains 2 scripts
CheckForFiles - Checks the amount of files in a given Google Drive folder before releasing the execution of other scripts.
SheetToCSV - Creates a .csv file of the sheet in the parent folder of that sheet.
This script is applied to the spreadsheet's Submit button.
2 - Upload.gs | Contains some functions responsible for uploading files through the spreadsheet.
ShowDialog0101 - Basically it's a script to call the upload page through an HTML alert in the spreadsheet.
GetParent - Basically it's a script that discovers the ID of the spreadsheet's parent folder and passes this information to the HTML file. I created this function because that way I can use this worksheet's folder as a model folder, simply duplicating the entire content without having to edit the code to update the worksheet's folder ID.
CreateOrGetFolder - This is the main function of the upload script, it checks if there is a child folder that has the name "Video" inside the parent folder, if it exists, it takes the ID of that folder so that the file is uploaded in that folder, if it does not exist, it creates a folder called "Video" and takes the ID of the created folder.
This is the Web App that launches when the Video File button is clicked:
Web App - Stack Demonstration
HTML file
Basically contains the client-side upload functions, I adapted this script based on this one.
What i would like to do
As I commented initially, I would like to adapt these scripts to work in a Web App.
My idea is that instead of the person filling out the worksheet, they fill out a form.
For this I need to adapt mainly the SheetToCSV script to link with a Submit button in the Web App, the idea is that as soon as the form is completed and the file upload is completed, this button is released and then when clicking on it the SheetToCSV script be triggered by creating a .csv file in the spreadsheet's parent folder with all the form responses.
My main difficulty is in linking the .csv generation script with the Submit button, I've been racking my brains over this for days.
I'm already having nightmares with this programming, literally... if anyone can help me with this, I'd be very grateful!
EDIT
I'll try to explain in a little more detail here.
Currently, I have this google spreadsheet here:
This worksheet contains modified versions of 2 scripts created by Tanaike.
Script 1 - Generates a .csv file with the worksheet fields in the same folder as the worksheet.
Script 2 - It is a modified version of Tanaike's Resumable Upload for Web Apps script, it is called in the spreadsheet via html alert.
Resumable Upload for Web Apps via HTML Alert
CSV file generated by the worksheet
Everything works as expected in this worksheet, but now I would like to convert it to a Web App, like this example:
The issue is that I don't know how to convert Tanaike's .csv generation script to generate the files through this html form, what I need is to integrate it with the Submit button of the Web App and collect the form fields in a .csv file.
The Spreadsheet and the Web App can be viewed at these links:
Google Sheet
Web App Form
Thank you for replying. Can I ask you about your expected values for the CSV data? In this case,
I confirmed your expected result in this question as follows.
You want to retrieve 2 text values of "name01", "description", and 2 values from dropdown lists of "Option1" and "Option2". The total values are 4 values.
When the HTML form is submitted, you want to create a new CSV file for every submission.
In this case, how about the following modification? Unfortunately, in your question, your script is not shown. So, in this answer, I would like to propose a simple modification.
When I saw your sample Spreadsheet including your script, when the submit button is clicked, it seems that the function submitForm() is run. In this answer, this is used.
Modified script:
Javascript side:
Please modify submitForm() as follows.
function submitForm() {
// Added the below script.
var name = $('#name01').val();
var description = $('#description').val();
var option1 = $('#Option1').val();
var option2 = $('#Option2').val();
var data = [name, description, option1, option2].join(",");
google.script.run.saveDataAsCSV(data, uploadParentFolderId);
if ($('#submit-btn.disabled')[0]) return; // short circuit
Google Apps Script side:
Please add the following function. Please modify the filename of "sample.csv" to your actual situation.
const saveDataAsCSV = (data, folderId) => DriveApp.getFolderById(folderId).createFile("sample.csv", data);
By this modification, 4 values in HTML form are retrieved and save it as a CSV file to the folder of uploadParentFolderId.
If you want to save the file to other folder, please modify uploadParentFolderId of google.script.run.saveDataAsCSV(data, uploadParentFolderId).
I am trying to make a spreadsheet capable of opening an HTML file as a sidebar, but the HTML file is located on a master spreadsheet. Here is the code that I have already for opening the sidebar using an HTML that is in the spreadsheet.
function sidebar1()
{
var b1 = SpreadsheetApp.getActiveSheet().getRange('C13').getValue();
html = HtmlService.createHtmlOutputFromFile(b1).setTitle(b1);
ui.showSidebar(html);
}
It's only possible if you are the current user of that spreadsheet and that spreadsheet is the container of the script because SpreadsheetApp.getUi() as described in the documentation Returns an instance of the spreadsheet's user-interface environment that allows the script to add features like menus, dialogs, and sidebars. if a spreadsheet is opened by id or url then there is no user interface
I just thought that if you have two spreadsheets already open it might be possible to call a server side function on one to run a server side function on the other using Apps Script API which might be able to launch a Sidebar on the second sheet. So maybe I'm wrong. Give it a try.
One solution would be to open the HTML file into a temporary workbook, and copy the sheet from there into the workbook containing all of them.
I'm trying to figure out how to check a Google Drive URL to see if a specific Gmail account has 'edit rights' to it. Is this possible in Apps Script? If yes, any help is greatly appreciated.
I'm currently researching File.getEditors(), but not sure how to test this code:
// Log the email address of all users who have edit access to a file.
//https://developers.google.com/apps-script/reference/drive/user
var file = DriveApp.getFileById('1mgw9xYO7X99brzrIk4Uel8YzndQa8dpGGQCnrFn4suU');
var editors = file.getEditors();
for (var i = 0; i < editors.length; i++) {
Logger.log(editors[i].getEmail());
}
To test this code, you need to place the code inside a a function inside the Script Editor on Google Drive, then execute the function.
You can access the Script Editor in two ways, either from within a spreadsheet (under Tools->Script Editor), in which case your script will be bound to the Spreadsheet. Binding to a spreadsheet can be useful if you want the script to work with data from sheet, for example you might want to store some configuration settings in the sheet, in which case keeping the sheet and the code together as one file is convenient.
You can also create a stand alone Script by connecting the Google Apps Script app to your Google Drive (+New -> More -> Connect more apps), then creating a stand alone Google Apps Script file directly on drive.
Regardless of how you create the script, the behaviour of this script and the script editor will be exactly the same.
Once you are in the script editor, paste that code into a function (by default you will be given an empty function named "myFunction()". Then run it using the "Run" menu.
Finally, under the "View" menu, go to "Logs" to see the results of Logger.log().
I want to dynamically manage and update a section of a Google Sites page (think HTML).
I understand this needs to be done via a Gadget (to get it on the page), Google Script, and via HTMLService.
The Google Script Editor has a .gs tab and HTML tab for writing the script and HTML. However, I want to separately manage and update the HTML in a document on Google Drive, and have the script use that to feed the HTML back to the page.
Is there a way to reference the document to .createHTMLOutputFromFile(), so that I can have it use the HTML in the doc on Google Drive, rather than having to compose HTML in the Google Script Editor? Please see .gs function example below:
return HtmlService.createHtmlOutputFromFile
(replace **'Index'** with **'Google Docs URL to Google Docs HTML document'**);
I tried different methods for providing the URL, but the script editor can never find the HTML document using the URL I provide. If this is possible, where can I find an example of how to provide the proper URL?
I'd use HtmlService.createHtmlOutput and stream the text in.
var template_welcome = HtmlService.createTemplateFromFile('welcome_template_view');
return template_welcome.evaluate().setTitle("Template Welcome");
that is an example of how to use an html file that is in your google scripts directory. The welcome_template_view is an html file in my directory.
I have a google spreadsheet that imports and modifies data from an online source that updates every hour. I wish to pull a static copy of the 10pm update from the spreadsheet—essentially a copy-paste special at a certain time. I am using the following Script and set a Time-Driven trigger for 10pm. The script pulls data from cells N5:N10 of one sheet (which update every hour) and pastes only static values into cells N5:N10 of a second sheet in the same spreadsheet.
function PasteValue()}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var data= ss.getSheets()[1].getRange("N5:O10").getValues();
ss.getSheets()[2].getRange("N5:O10").setValues(data);
}
When I run the script manually it works perfectly. However, when I set the Time-Driven trigger and close the sheet, all that is imported is a series of static “#######” when I check the next morning.
Does anybody have any insight as to the problem? Or, if anybody has an alternative script or method to automatically copy-pastevalues in google spreadsheets using formulas or scripts, I would really appreciate it.
Thanks,
Ron
When using triggers openById instead of getActive
function PasteValue()}
var ss = SpreadsheetApp.openById(id);
ss.getSheets()[1].getRange("N5:O10").copyTo(ss.getSheets()[2].getRange("N5"),{contentsOnly:true});
}
I think the formula that you are using (importdata or importhtml) is not calculating when the spreadsheet is not open i.e. user is not online.
You can use a script with urlfetchapp.