I'm still SUPER new to Google Sheets, I've mostly just programmed in VBA before this.
The project I'm working on is for a D&D type game, wherein the DM will use Sheet to record damage dealt to the monster. Basically the DM types the damage, clicks the calculate button, then the code sums up the current damage to baddie.
The code works perfectly. The problem I'm having is a human one. There is a tendency to click the "calculate" button before closing the cell.
For instance, the DM types 31 in the cell and immediately clicks "calculate" before hitting enter. This prevents the damage from being recorded properly and I can't figure out the code to make the cell close first.
Here is my current code:
function DamageRecordEnemy1Copy1() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1').activate();
var DamageLocation = spreadsheet.getRange('Q4');
var DamageLogLocation = spreadsheet.getRange('Q5');
var DamageLocationValue = DamageLocation.getValue();
var DamageLogLocationValue = DamageLogLocation.getValue();
var NewDamage = DamageLocationValue + DamageLogLocationValue;
DamageLocation.activate();
spreadsheet.getCurrentCell().setValue(NewDamage);
DamageLogLocation.activate();
spreadsheet.getCurrentCell().setValue("");
spreadsheet.getRange('A1').activate();
}
Any idea how to solve the issue? Much appreciated!
Thanks for the help everyone, here is what I ended up doing:
function onEdit(e){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = e.range;
var DamageLocation = spreadsheet.getRange('Q4');
var DamageLogLocation = spreadsheet.getRange('Q5');
var DamageLocationValue = DamageLocation.getValue()
var DamageLogLocationValue = DamageLogLocation.getValue()
var allRows = [5];
var allColumns = [17, 20, 23, 26, 29, 32];
var EditRow = range.getRow()
var EditColumn = range.getColumn()
if (allColumns.indexOf(EditColumn) > -1)
{
if (allRows.indexOf(EditRow) > -1)
{
var BadDamage = spreadsheet.getRange(EditRow-1,EditColumn)
var BadDamageLog = spreadsheet.getRange(EditRow,EditColumn)
var DamageCalc = BadDamage.getValue() + BadDamageLog.getValue()
BadDamage.setValue(DamageCalc);
BadDamageLog.setValue("");
}
}
};
Replace the "button" with insert->checkbox then use the OnEdit function to check if that cell was set to true, execute your button code and set it back to false. The reason this will work is because Drawings are on it's own layer but the checkbox will force them to have the "hit enter" behavior.
Related
I'm trying to get this code to work but keep getting a "TypeError: offset.copyTo is not a function". The script is supposed to take one value from sheet "Budget" and copy it into the next available row in column F on sheet "Projected".
I have tried playing around with syntax and other ways to copy but to now avail, any help would be greatly appreciated
function OffsetRecord() {
var sheet = SpreadsheetApp.openById("xx").getSheetByName("Budget");
var offset = sheet.getRange('I5').getValue();
var destSheet = SpreadsheetApp.openById("xx").getSheetByName("Projected");
var colF = destSheet.getRange('F:F').getValues();
var fcolF = colF.filter(function (e) {return e[0];});
var firstEmptyRowIncolF = fcolF.indexOf('')+1;
offset.copyTo(firstEmptyRowIncolF, {contentsOnly: true});
copyTo expects ranges as parameters, not values or column numbers, see here
Modify your code as following:
function OffsetRecord() {
var spreadsheet = SpreadsheetApp.openById("XX");
var sheet = spreadsheet.getSheetByName("Budget");
var offset = sheet.getRange('I5');//without ".getValue();"!
var destSheet = spreadsheet.getSheetByName("Projected");
...
firstEmptyRowIncolF = ...;
var destinationColumn = 1;
var range = destSheet.getRange(firstEmptyRowIncolF, destinationColumn);
offset.copyTo(range, {contentsOnly: true});
}
I have this script here, but I want it to only run on ONE specific sheet named "Nes Smart Data" (SheetNo5). Currently it runs on all sheets and puts the data on cells where I don't want them to be. Can you help me correct the code? Thanks a lot!
function onEdit(e){
var sheet = SpreadsheetApp.getActiveSheet();
var range = e.range;
var column = range.getColumn();
var row = range.getRow();
var text;
if (column==11) { //Replace 2 with the column number of your comments cell//
var newRange = sheet.getRange(row,column-5); //If the date is in the next column
var today = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'dd.MM.yyyy');
newRange.setValue(today);
}
if (column == 11){
text = sheet.getRange(row, 23).getValue();
sheet.getRange(row, 23).setValue(text + e.value+".");
}
}
Replace the following variable and things should work as desired -
Current:
var sheet = SpreadsheetApp.getActiveSheet();
Modification proposed:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Nes Smart Data');
Let me know if it doesn't!
So I am new to Google Sheets. I am making an inspection report for a small company using Sheets. I would like to add a button to the bottom of the sheet which does the following script:
Duplicate original sheet
Rename sheet to a cell value +today's date. i.e Fred01011980
Email the sheet as a PDF to a recipient.
Finally clear the inputted values in the original master template sheet.
I have looked up how to do each of these and a few of them are straightforward, but I don't know how to combine them. Can I just add all of the functions individually together without any additional syntax needed? Any help on this would be really appreciated. Thank you.
I believe you can just create another function that calls your functions, like this:
function doAllStuff() {
doStuff();
doMoreStuff();
}
Here's how I did it:
function AllinOne() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var name = "Inspection";
var sheet = ss.getSheetByName("Inspection").copyTo(ss);
var newname = ss.getSheetByName("Inspection").getRange(2, 2).getValue();
var newnamedate = ss.getSheetByName("Inspection").getRange(3, 2).getValue();
var sheetx = ss.getActiveSheet()
var thisSheet = sheetx.getName();
var actualSheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName()
var sheetfinal = ss.getSheetByName("Inspection")
var old = ss.getSheetByName(newname+newnamedate);
if (old) ss.deleteSheet(old);
sheet.setName(newname+newnamedate); //copies the sheet and renames it to "store+date"
ss.setActiveSheet(sheet)
// below is pdf conversion
var originalSpreadsheet = SpreadsheetApp.getActive();
var sourcesheet = originalSpreadsheet.getActiveSheet();
var sourcerange = sourcesheet.getRange('A1:B176'); // range to get - here I get all of columns which i want
var sourcevalues = sourcerange.getValues();
var data = sourcesheet.getDataRange().getValues();
var newSpreadsheet = SpreadsheetApp.create(thisSheet); // can give any name.
var sheetnow = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var projectname = SpreadsheetApp.getActiveSpreadsheet();
var sheetz = sourcesheet.copyTo(newSpreadsheet);
var destrange = sheetz.getRange('A1:B176');
destrange.setValues(sourcevalues);
newSpreadsheet.getSheetByName('Sheet1').activate();
newSpreadsheet.deleteActiveSheet();
var pdf = DriveApp.getFileById(newSpreadsheet.getId());
var theBlob = pdf.getBlob().getAs('application/pdf').setName(newname+newnamedate);
var folderID = "File_ID"; // Folder id to save in a folder.
var folder = DriveApp.getFolderById(folderID);
var newFile = folder.createFile(theBlob);
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
MailApp.sendEmail("Email","New Manager Inspection: "+newname+newnamedate, "A new Manager Inspection Report has been added to the Drive.");
ss.getSheetByName(name).getRangeList(["B176","B150:B164","B138:B146","B129:B134","B119:B125","B106:B115","B99:B102","B84:B95","B78:B80","B66:B74","B55:B62","B48:B51","B34:B44","B25:B30","B18:B21","B10:B14","B4:B7","B2","B16","B23","B32","B46","B53","B53","B64","B76","B82","B97","B104","B117","B127","B136","B148","B166"]).clearContent();
ss.setActiveSheet(sheetfinal);
SpreadsheetApp.getUi().alert("Report Submitted, Please do NOT Resubmit. Thank You");
}
I'm trying to create an app script that you can type in the name and username of an individual and select various dates associated with their training. I'm nearly done with the project but one piece of script is giving me issues. What the script is supposed to do is take the User Name I've entered into the app and see if that value already exists in column L of the archive. If the value exists then it should return an alert with a YES_NO option and depending on what is selected it will either cancel the import or continue on and import the username to the sheet.
I've been trying to figure out this script for about 5 days and cannot figure it out. I've tried pretty much everything I can think of. Here's the code. The Username already exists in Cell L3 of the Archive.
function postApp(e)
{
var usern = e.parameter.userN;
var name1 = e.parameter.name;
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var tM = sheet.getSheetByName('Archive');
var tR = tM.getLastRow()-1;
var tC = tM.getLastColumn();
var tD = tM.getRange("L3").getValues();
if(tD == usern )
{
var ui = SpreadsheetApp.getUi(); // Same variations.
var result = ui.alert('Wait', 'The Username you are trying to use has already been archived. Would you still like to continue?', ui.ButtonSet.YES_NO);
// Process the user's response.
if (result == ui.Button.Yes) {
// User clicked "Yes".
SpreadsheetApp.getActiveSpreadsheet().toast('Error Report Has Been Updated', 'UPDATE ERRORS',3);
var date = new Date();
var d = Utilities.formatDate(date, Session.getScriptTimeZone(), 'M/d/yyyy');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('SyNERGY');
var lR = s.getLastRow()+1;
var lC = s.getLastColumn();
var app = UiApp.getActiveApplication();
var name = e.parameter.name;
var user = e.parameter.userN;
var team = e.parameter.team;
var hire = e.parameter.hire;
////////////////////////////
// CREATE REPORT CARD
genReportCard(name, team, hire, app, user);
s.getRange(lR,12).setValue([[user]]).setNumberFormat(';;;');
s.getRange(lR,12).setValue(user.toString().toLowerCase());
s.getRange(lR,11).setValue('SyN');
///////////////////////////////////////////
///////////Close after complete////////////
var ui = SpreadsheetApp.getUi(); // Same variations.
var result = ui.alert("Specialist Created!");
var app = UiApp.getActiveApplication();
app.close();
return app;
}else {
// User clicked "No" or X in the title bar.
ui.alert('Import Canceled');
}
}else{
var date = new Date();
var d = Utilities.formatDate(date, Session.getScriptTimeZone(),
'M/d/yyyy');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('SyNERGY');
var lR = s.getLastRow()+1;
var lC = s.getLastColumn();
var app = UiApp.getActiveApplication();
var name = e.parameter.name;
var user = e.parameter.userN;
var team = e.parameter.team;
var hire = e.parameter.hire;
////////////////////////////
// CREATE REPORT CARD
genReportCard(name, team, hire, app, user);
s.getRange(lR,12).setValue([[user]]).setNumberFormat(';;;');
s.getRange(lR,12).setValue(user.toString().toLowerCase());
s.getRange(lR,11).setValue('SyN');
///////////////////////////////////////////
///////////Close after complete////////////
var ui = SpreadsheetApp.getUi(); // Same variations.
var result = ui.alert("Specialist Created!");
var app = UiApp.getActiveApplication();
app.close();
return app;
}
}
This script Works as long as I am referencing One Cell so tM.getRange("L3").getValues(); Works. However, As soon as I change that to tM.getRange("L3:L").getValues(); it does not recognize the value exists within the range and returns the else portion of the code.
I have also tried a loop for the top section that is giving me issues but the same thing is happening. As soon as I try to find a value within the column rather than a specific cell it won't work. Here is what it looks like with the loop.
function postApp(e)
{
var usern = e.parameter.userN;
var name1 = e.parameter.name;
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var tM = sheet.getSheetByName('Archive');
var tR = tM.getLastRow()-1;
var tC = tM.getLastColumn();
var tD = tM.getDataRange().getValues();
for(var i = 0; i<tD.length; i++){
if(tD[i][11] == usern )
{
If anyone can figure out what i'm doing wrong or why it's not working as soon as I reference the entire column any help would be greatly appreciated.
Thanks!
I'm trying to create a script in google sheets. What I'm trying to do is to search the value of a cell (let's say A2 sheet1), then change the value of another cell (B2 sheet2). When the first iteration is over, proceed to the next row (A3 sheet1) and again change the value of B2. Also wait 10 secs at the end of every iteration.
I don't know why it is not working. After I run the code it just says "Finished", but nothing has changed.
function test() {
var ss = SpreadsheetApp.getActive();
var gensheet = ss.getSheetByName('Generador SE');
var clientsheet = ss.getSheetByName('Clientes SE');
var clientrange = clientsheet.getRange("A1:C200");
var clientsquantity = clientrange.getNumRows()
var servicetochange = gensheet.getRange(2,2)
var i;
for (i=1;i>clientsquantity;i++){
var currentservice = clientrange.getRange(i,1).getValue;
servicetochange=gensheet.getRange().setValue(currentservice);
Utilities.sleep(10000);
}
}
I have changed some code in the loop ... now it does something (but I can't be sure it does what you expect):
function test() {
var ss = SpreadsheetApp.getActive();
var gensheet = ss.getSheetByName('Generador SE');
var clientsheet = ss.getSheetByName('Clientes SE');
var clientrange = clientsheet.getRange("A1:C200");
var clientsquantity = clientrange.getNumRows()
var servicetochange = gensheet.getRange(2,2)
var i;
for (i=1;i<clientsquantity;i++){
var currentservice = clientrange.offset(i,0,1,1).getValue();
servicetochange.offset(i,0,1,1).setValue(currentservice);
Utilities.sleep(10000);
}
}