Google Spreadsheet unable to deploy web app script -- - google-apps-script

I've been trying to deploy this web app bound to my shared spreadsheet. Basically it's a script that locks a cell after it's been edited. It is working just fine when I access it, however when I switch to another user and try, it doesn't function. You would think it would be a sharing permission setting, but I've changed everything to public and still no luck. Can you someone please help?
Here is the script:
function doGet(e) {
var sheet = SpreadsheetApp.openById('1234567891011');
}
function onEdit(){
var masterSheetName = "PlaceYourSquare" // sheet where the cells are protected from updates
var helperSheetName = "Helper" // sheet where the values are copied for later checking
var firstDataRow = 1; // only take into account edits on or below this row
var firstDataColumn = 1; // only take into account edits on or to the right of this column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = ss.getActiveSheet();
if (masterSheet.getName() != masterSheetName) return;
var masterCell = masterSheet.getActiveCell();
if (masterCell.getRow() < firstDataRow || masterCell.getColumn() < firstDataColumn) return;
var helperSheet = ss.getSheetByName(helperSheetName);
var helperCell = helperSheet.getRange(masterCell.getA1Notation());
var newValue = masterCell.getValue();
var oldValue = helperCell.getValue();
if (oldValue == "") {
helperCell.setValue(newValue);
} else {
masterCell.setValue(oldValue);
}
}

Related

Run a function with onEdit on Google Sheets mobile app

I made my function that it's working good for me now, but i have the issue to make it work on mobile so i tried another way to use the function OnEdit, so when it will be the value that i want to lunch the function that i created before, but for now it's not working and i don't know why it's not, i'm asking for you help with this small issue ;)
thank you
function onEdit(e) {
var range = e.range;
var spreadSheet = e.source;
var sheetName = spreadSheet.getActiveSheet().getName();
var column = range.getColumn();
var row = range.getRow();
var value = SpreadsheetApp.getActiveSheet().getRange(row, column).getValue();
if(sheetName == 'New Orders' && column == 12 && value=='COMMANDE VALIDER')
{
VALIDERCOMMANDE();
}
}
function VALIDERCOMMANDE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("New Orders");
var url = "https://docs.google.com/spreadsheets/d/1eTWG_XZt-3CMzkxgKM4pCvD41deGMdka37eQkHM9oDg/edit#gid=0";
var ss2 = SpreadsheetApp.openByUrl(url);
var pasteSheet = ss2.getSheetByName("Order Pull");
// get source range
var max = copySheet.getMaxRows().toString();
var range = copySheet.getRange(2, 1, max, 12);
var dataValues = range.getValues();
for (i = 1; i < dataValues.length; i++) {
if (dataValues[i][11] === 'COMMANDE VALIDER') {
pasteSheet.appendRow([dataValues[i][0],
dataValues[i][1],
dataValues[i][2],
dataValues[i][3],
dataValues[i][4],
dataValues[i][5],
dataValues[i][6],
dataValues[i][7],
dataValues[i][8],
dataValues[i][9],
dataValues[i][10],
dataValues[i][11]]);
var clearRow = i + 2;
copySheet.getRange('D' + clearRow + ':L' + clearRow).clearContent();
}
}
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, max, 1);
// clear source values
Browser.msgBox('Commande Confirmer');
}
Try this way
function onMyEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'New Orders' && e.range.columnStart == 12 && e.value == 'COMMANDE VALIDER') {
VALIDERCOMMANDE();
}
}
function VALIDERCOMMANDE() {
var ss = SpreadsheetApp.getActive();
var csh = ss.getSheetByName("New Orders");
var id = "1eTWG_XZt-3CMzkxgKM4pCvD41deGMdka37eQkHM9oDg";
var ss2 = SpreadsheetApp.openById(id);
var psh = ss2.getSheetByName("Order Pull");
var vs = csh.getRange(2,1,csh.getLastRow() - 1, 12).getValues().filter(r => r[11] == 'COMMANDE VALIDER').filter(e => e);
psh.getRange(psh.getLastRow() + 1, 1, vs.length, 12).setValues(vs);
}
I often find that onEdits are not reliable on mobile and sometimes the trigger process has to be repeated
Class Browser, Class UI and SpreadsheetApp.toast don't work in the Google Sheets mobile apps (iOS and Android). By the other hand, instead of using a simple trigger you should use an installable trigger because SpreasheetApp.openByUrl method requires authorization to run.
Change the name of the onEdit function, remove Browser.msgBox('Commande Confirmer'); and create an installable on edit function calling the renamed function should make your script work on the mobile apps
If you really need to have a custom notification when the onEdit function finish, you might send write a message or image on certain range. If you use on edit installable trigger you might also send an email or call an external API.
Related
Executing Google Apps Script Functions from Mobile App
Google Apps Script toast messages don't appear for anonymous editors
why is my trigger status "Paused" when triggered from mobile

How to protect ranges per specific users in google sheet?

I am trying to apply protection on all sheets and certain ranges using a google script, for every sheet in its workbook (and can be applied on new added sheets via a trigger later)
the problem is, I want to assign different ranges to different users after protecting the whole sheet from anyone, but the people allowed to edit on their ranges!
I can't seem to know why it doesn't work.. It works oppositely, allowing all users to edit all ranges but the protected ones.
I want the first editor to be able to edit his QC_Range,
and the second editor to be able to edit his PLN_Range
and restrict access from editing (making it view only) for all other cells and whole the sheet.
function Sheet_Ranges_Protection(){
var Veranda_Test = SpreadsheetApp.openById("Sheet ID");
//var workbookB = SpreadsheetApp.getActiveSpreadsheet();
var Veranda_Sheets = Veranda_Test.getSheets();
for(var SheetNumb = 0; SheetNumb < Veranda_Sheets.length; SheetNumb++)
{
var Shprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.SHEET);
var QC_Range = Veranda_Sheets[SheetNumb].getRange("E1:G5");
var PLN_Range = Veranda_Sheets[SheetNumb].getRange("A1:C5");
if (Shprotection == true)
SheetNumb++;
else if (Shprotection == false)
{
var Shprotection = Veranda_Sheets[SheetNumb].protect().setDescription('Sheet Protection');
var Rangesprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (Rangesprotection == false)
{
var QCprotection = QC_Range.protect().setDescription('QC Protection');
var me = Session.getEffectiveUser();
QCprotection.addEditor(me);
QCprotection.removeEditors(QCprotection.getEditors());
if (QCprotection.canDomainEdit())
{
QCprotection.setDomainEdit(false);
QCprotection.addEditors(['Editor1#gmail.com']);
}
var PLNprotection = PLN_Range.protect().setDescription('PLN Protection');
var me = Session.getEffectiveUser();
PLNprotection.addEditor(me);
PLNprotection.removeEditors(PLNprotection.getEditors());
if (PLNprotection.canDomainEdit())
{
PLNprotection.setDomainEdit(false);
PLNprotection.addEditors(['Editor2#gmail.com']);
}
}
else
SheetNumb++;
// Shprotection = true;
// Rangesprotection = true;
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script will throw an exception upon removing the group.
}
}
}
I've reworked your code and I think it does what you pretend now. In this case, I don't make a sheet protection but protect all the ranges separately:
function Sheet_Ranges_Protection() {
var Veranda_Test = SpreadsheetApp.openById("Sheet ID");
var Veranda_Sheets = Veranda_Test.getSheets();
for(var SheetNumb = 0; SheetNumb < Veranda_Sheets.length; SheetNumb++) {
var me = Session.getEffectiveUser();
// Define ranges that will be protected for everyone
var range1 = Veranda_Sheets[SheetNumb].getRange(6, 1, Veranda_Sheets[SheetNumb].getMaxRows(), Veranda_Sheets[SheetNumb].getMaxColumns());
var range2 = Veranda_Sheets[SheetNumb].getRange(1, 8, 5, Veranda_Sheets[SheetNumb].getMaxColumns());
var range3 = Veranda_Sheets[SheetNumb].getRange(1, 4, 5);
var ranges = [range1, range2, range3];
// Set protection for all the sheet minus QC/PLN ranges
for(var i = 0; i < ranges.length; i++) {
var rangeProtection = ranges[i].protect().setDescription('Range protection');
rangeProtection.addEditor(me);
rangeProtection.removeEditors(rangeProtection.getEditors());
if (rangeProtection.canDomainEdit()) {
rangeProtection.setDomainEdit(false);
}
}
var QC_Range = Veranda_Sheets[SheetNumb].getRange("E1:G5");
var PLN_Range = Veranda_Sheets[SheetNumb].getRange("A1:C5");
// Set protection for QC range
var QC_protection = QC_Range.protect().setDescription('QC protection');
QC_protection.removeEditors(QC_protection.getEditors());
QC_protection.addEditor('Editor1#gmail.com');
if (QC_protection.canDomainEdit()) {
QC_protection.setDomainEdit(false);
}
// Set protection for PLN range
var PLN_protection = PLN_Range.protect().setDescription('PLN protection');
PLN_protection.removeEditors(PLN_protection.getEditors());
PLN_protection.addEditor('Editor2#gmail.com');
if (PLN_protection.canDomainEdit()) {
PLN_protection.setDomainEdit(false);
}
}
}
I hope this works for you!

Google App Script - getCommenter

I'm brand new to Google Apps Script, and I'm trying to create a simple spreadsheet that will allow me to share files by user email through a single spreadsheet.
I have written the following script, which will allow me to add editors and viewers, but not commenters.
I keep getting an error that states that the function addCommenter cannot be found in object spreadsheet.
function shareSheet () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.toast('Updating access level...');
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
var range1 = sheet.getRange (3,1, lastRow,9);
var data = range1.getValues();
for (var i= 0; i < data.length; i++) {
var row = data [i];
var accessLevel = row [5];
var values = row [8];
var ss2 = SpreadsheetApp.openById(values);
var values2 = row [4];
// Add new editor
if (accessLevel == 'Edit') {
var addEditors = ss2.addEditor(values2);
}
// Add new viewer
if (accessLevel == 'View'){
var addViewers = ss2.addViewer(values2);
}
// Add new commenter
if (accessLevel == 'Comment') {
var addCommenters = ss2.addCommenter(values2);
}
}
}
The Spreadsheet object does not support the addCommentor() method. You should use DriveApp service instead.
DriveApp.getFileById(id).addCommenter(emailAddress)

function onEdit() runs only partially

I have a very frustrating problem on my hands and I turn to you for help once more. I had the onEdit() function below which, together with the auxiliary functions, worked fine.
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var activeCell = activeSheet.getActiveCell();
//Check if the sheet is a JOb sheet and the cell us the status cell
if ( activeSheet.getName().indexOf("Job ID") != -1 && activeCell.getRow() == 4 && activeCell.getColumn() == 15 ) {
var targetSheet = ss.getSheetByName('Active Jobs');
var jobRowNumber = findJobIdRow();
var sourceCell = activeSheet.getRange(4,15);
sourceCell.copyTo(targetSheet.getRange(jobRowNumber,16));
}
if (activeSheet.getName().indexOf("Job ID") != -1 && activeCell.getRow() == 2 && activeCell.getColumn() == 15){
var switchValue = activeCell.getValue();
switch (switchValue){
case "On hold (i)":
case "On hold (ii)":
case "On hold (iii)":
case "To be assigned":
//Write date to active jobs sheet
addDateToActive("TBC");
break;
case "In progress":
var newDate = Browser.inputBox("Please enter report out date, example 18-Aug-2017");
addDateToActive(newDate);
break;
//default:
//Browser.msgBox("GOTHERE");
}
}
}
function findJobIdRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var jobID = ss.getActiveSheet().getRange(2,1).getValue();
var column = ss.getSheetByName('Active Jobs').getRange(2,1,ss.getSheetByName('Active Jobs').getMaxRows()-2,1);
var values = column.getValues(); // get all data in one call
for(var ct = 0; ct < values.length-1; ct++){
if(values[ct][0] == jobID){
var ct = ct+2;
break;
}
}
return ct;
}
function addDateToActive(input){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var activeCell = activeSheet.getActiveCell();
var jobid = activeSheet.getRange(2,1).getValue().toString();
var activeJobSheet = ss.getSheetByName("Active Jobs");
var activeJobs = activeJobSheet.getRange(1,1,activeJobSheet.getLastRow(),1).getValues();
activeJobs = ColumnToArray(activeJobs);
var jobrow = activeJobs.indexOf(jobid)+1;
if (jobrow == -1){
Browser.msgBox("Job Id not preent on Active Jobs sheet");
}else{
activeJobSheet.getRange(jobrow,15).setValue(input);
}
}
Then I included some code in this script which was supposed to send out some e-mails to people if some dates were approaching today's date. There were some problems with that code because of authorization requirement so I moved it into it's own separate function and came back to the original script that is posted above. Now the problem I am facing is, although this script works fine if ran from the script editor, manually run from a drawing button in the spreadsheet, or is ran by a on edit trigger I set up from the "Current project triggers" menu, it will not do the entire script if the script is triggered by the onEdit() function name. It does the first bit where it copies the content of a cell across but not the second bit with the case switch.
The obvious fix would be to just set up the trigger from the "Current project triggers" but this onEdit detection needs to apply to anyone in my team that edits this sheet. If I set it up like that it will work for me but no one else from my team.
Any help would be appreciated.
Setting up the trigger via the function name onEdit() proving tricky I wrote this bit of code and attached it to a drawing in the sheet. Then instructed all the users to click it which set up the trigger for them. This solved the problem and the trigger works fine now.
function triggerSetUp(){
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("enforcer").forSpreadsheet(sheet).onEdit().create();
}

Google Sheets Add-On onEdit(e) not working with Enabled Document

I am trying to to create an Add On where the user has the option to either choose from the menu, or an onEdit command triggers a change to the cell in sheets. When I use the code below, attached to the sheet it works perfectly, however, when i test it as AUTH-LIMITED (Enabled or installed & enabled) the onEdit(e) functionality doesn't work. Everything about the menu button is working great, I can't figure out how to get the onEdit(e) called, what so ever.
I've tried searching all over but to no success with how to solve for this specific issue.
Thank you in advance!
function onEdit(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheeter = ss.getActiveSheet();
sheeter.getRange("a1").setValue("Test");
var range = e.range;
var val = range.getValues();
if(val.length+val[0].length<=2){
var val = range.getValue();
if(range.getFormula()){}
else{
var regex2 = new RegExp('[0-5]{0,1}[0-9]:[0-5]{0,1}[0-9].[0-5]{0,1}[0-9]$','g');
var docContent2 = val.replace(regex2,"00:"+val);
range.setValue(docContent2);
range.setNumberFormat("[M]:SS.0");
}
}
}
function Mass_Convert(){
var now = Date.now();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getActiveRange();
var val = range.getValues();
var replaceBox = new Array(val.length);
var replaceFormat =range.getNumberFormats();
for(var i=0;i<val.length;i++){
replaceBox[i] = new Array(val[i].length);
replaceFormat[i] = new Array(val[i].length);
for(var j=0;j<val[0].length;j++){
var newRange = range.getCell(i+1,j+1);
var newVal = newRange.getValue();
var format = newRange
if(newRange.getFormula()){
replaceBox[i][j]=newRange.getFormula();
replaceFormat[i][j] = newRange.getNumberFormat();
}
else{
if(isNaN(newVal)){
var regex2 = new RegExp('[0-5]{0,1}[0-9]:[0-5]{0,1}[0-9].[0-5]{0,1}[0-9]$','g');
var docContent2 = newVal.replace(regex2,"00:"+newVal);
replaceBox[i][j] = docContent2;
replaceFormat[i][j] = "[M]:SS.0"
}else{
replaceBox[i][j] = newVal;
replaceFormat[i][j] = newRange.getNumberFormat();
}
}
}
}
range.setValues(replaceBox);
range.setNumberFormats(replaceFormat);
}
function onOpen(e) {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Split Conversion')
.addItem('Mass Convert Selected', 'Mass_Convert')
.addToUi();
}
function onInstall(e) {
onOpen(e);
}
I tested this with my add-on and .getActiveSpreadsheet() gives the error:
Execution failed: The Add-on attempted an action that is not permitted in Test as Add-on mode. To use this action, you must deploy the Add-on
Also your onEdit() might have permission issues. There should be some condition to run on specific sheets otherwise it will run on all sheets.
I also found this:
onEdit(e) not working in Add-on