简体   繁体   中英

Google Apps Script Not Loading Functions for Everyone, only me

My onSheetOpen / showSidebar is not working for anyone but me when opening my events Google sheet. I have set up triggers to launch both the onSheetOpen and showSidebar for myself, but this is not working for anyone else who opens the spreadsheet. Can anyone help figure out why this might be the case? Below is the code I'm using.

 /**
 * Set up custom sidebar.
 * TRIGGER: spreadsheet event 'on open'
 */
function onSheetOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Actions')
    .addItem("Register interest in current opportunity", 'registerInterest')
    .addItem("Show sidebar", 'showSidebar')
    .addToUi();

  showSidebar();
}

/**
 * Renders the sidebar
 * TRIGGER: spreadsheet event 'on open' & custom menu option
 */
function showSidebar() {
  try {
    var htmlOutput = HtmlService.createHtmlOutputFromFile('sidebar');
    SpreadsheetApp.getUi().showSidebar(htmlOutput);
  } catch(e) {
    // Just ignore - means user doesn't have edit access to sheet
  }
}



/**
 * Main function. Grabs the required data from the currently-selected row, registers interest
 * in that data in a separate sheet, and sends a confirmation email to the user.
 */
function registerInterest() {
  //var spreadsheet = SpreadsheetApp.getActive();
  // These are the attributes we capture and store in the 'interest' sheet
  var who = Session.getActiveUser().getEmail();
  var when = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd HH:mm:ss');
  var data = getRowOfInterest(SpreadsheetApp.getActiveSheet());
  var id = Utilities.getUuid();
  Logger.log(id)
  if(data.what && data.what.length > 0) {
    // Get the Google Sheets ID of the target 'interest' sheet 
    var id = getConfigValue("InterestSheet");
    if(id) {
      // Now try and open that sheet for updating
      var ss = SpreadsheetApp.openById(id);
      if(ss) {
        var sheet = ss.getSheetByName('Interest');
        if(sheet) {
          // All good; log interest. First, create the new row of data for the target sheet
          sheet.appendRow([ when, who, data.what, data.subject,  data.location, data.industry, data.capabilities ]);
          // Second, grab our 'template' email from config & use that to send a confirmation 
          // email to the person registering their interest
          var body = getConfigValue('InterestEmail');
          MailApp.sendEmail(who, getConfigValue('InterestSubject'), null, { noReply: true, htmlBody: body });
        } else {
          throw Error('can\'t open the \'Interest expressed\' sheet (or it doesn\'t exist)');
        }
      } else {
        throw Error('can\'t open the \'Interest expressed\' sheet');
      }
    } else {
      throw Error('\'Interest expressed\' sheet not specified');
    }
  }
}

/**
 * Utility to derive the required data for registering interest.
 * Called from the registerInterest() function
 */
function getRowOfInterest(sheet) {
  var result = {};
  var row = sheet.getActiveRange().getRow(); // Get the currently-selected row index
  var fullRange = sheet.getRange(row, 1, 1, sheet.getLastColumn()); // Get the entire row of data
  //var fullRange = sheet.getRange(row, 1, 1, 9);
  Logger.log(fullRange.getValues())
  var data = fullRange.getValues();
  if(data[0] && data[0].length > 0) {
    for(var n = 0; n < data[0].length; n++) {
      // Populate specific attributes in the 'result' object - tailor this as you see fit
      if(n==0) result.subject = data[0][n];
      if(n==6) result.what = data[0][n] + ' ';
      //if(n==5 || n==7) result.what += data[0][n] + ' ';
      if(n==7) result.location = data[0][n];
      if(n==11) result.industry = data[0][n];
      if(n==12) result.capabilities = data[0][n];
    }

    result.what += '(' + sheet.getName() + ')';
    result.industry;
    result.capabilities;
    return result;
  }
}

function getSomeRange(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var row = sheet.getActiveRange().getRow(); // Get the currently-selected row index

  var myRange = sheet.getRange(row, 1, 1, sheet.getLastColumn());

  //Logger.log("Number of rows in range is "+myRange.getNumRows()+ " Number of columns in range is "+ myRange.getNumColumns());

}
/**
 * Utility to pull specified data from a config sheet. This assumes that a sheet called 'Config' 
 * is populated thus:
 *    Column contains a load of 'key' and column 2 contains the corresponding 'values'
 * Called from registerInterest() function
 */
function getConfigValue(key) {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Config');
  var result;

  if(sheet) {
    // Scan column 1 for key and return value in column 2
    var data = sheet.getDataRange().getValues();
    for(var n = 0; n < data.length; ++n) {
      if(data[n][0].toString().match(key)) result = data[n][1];
    }
  }

  return result;
}

function hideExpiredEvents() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Events');
  var exsh=ss.getSheetByName('Expired Events');
  var sr=2;
  var rg=sh.getRange(sr,1,sh.getLastRow()-sr+1,sh.getLastColumn());
  var vA=rg.getValues();
  Logger.log(vA);
  var dt=new Date();
  var  d=0;//deleted row counter
  var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate(0)).valueOf();
  for(var i=0;i<vA.length;i++) {
    if(new Date(vA[i][3]).valueOf()<today) {
      exsh.appendRow(vA[i]);//move to bottom of expired events
      sh.deleteRow(i+sr-d++);//increment deleted row counter
    }
  }
}

Unfortunately, it seems that the issue you are encountering might be a bug.

What you can do in this case is to star the issue on Issue Tracker here by clicking the next to the issue number and post a comment as well saying that you are affected by the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM