So I'm working on creating my own workout sheet to run on my mobile device b/c I don't like anything the app store offers.
Anyways, I want the sheet to automatically increase the weight being lifted based on a trigger. I've already got the sheet to return the next amount of weight to be lifted using formulas, now I want a trigger to run when the sheet calculates 25 reps to copy that increased value into the weight cell.
I'm trying to use the onEdit function. It runs without giving back an error, but nothing changes on the sheet.
Here's the code I have:
function myFunction() {
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if ("Data!D15" == "False") {
sheet.getrange("Sheet1!C4").setValue("Data!E15+0");
}
if ("Data!G15" == "0") {
sheet.getrange("Sheet1!C4").setValue("Data!D15+5");
}
}
}
Try this:
function onEdit(e) {
var sh1 = e.range.getSheet();
var sh2 = e.source.getSheetByName('Data');
if(!sh2.getRange('D15').getValue()){sh1.getrange("Sheet1!C4").setValue("Data!E15+0");}
if(sh2.getRange('G15').getValue()=="0"){sh1.getrange("Sheet1!C4").setValue("Data!D15+5");}
}
Related
I'm using Google Sheet's IMPORTDATA function to grab information from an XML file that is pulling from an API but the information I pull into the sheet isn't up to date.
How can I modify my sheet to pull in up-to-date data?
Compare the sheet: https://docs.google.com/spreadsheets/d/1W0Bt5z-Tky-tNhG_JtfE4FfjTRgQNRu_eQu2qVhQ-_E/edit?usp=sharing (LiveScores sheet)
To the XML: https://www67.myfantasyleague.com/2019/export?TYPE=liveScoring&L=64741&APIKEY=&W=14&DETAILS=1&JSON=0
Observe franchise id="0015" in both sets of data.
The sheet states <franchise id="0005" score="0.00" gameSecondsRemaining="21600" playersYetToPlay="6" playersCurrentlyPlaying="0" isHome="0">
The XML states <franchise id="0015" score="11.14" gameSecondsRemaining="20004" playersYetToPlay="4" playersCurrentlyPlaying="2"> (This data is for a football game that is currently being played as I'm writing this so the above example may not be exact, but it WON'T be score of 0.00, for example.
Any help would be amazing, thanks!
Have you tried using IMPORTXML? Google Sheets IMPORTXML Page
In IMPORTXML, you can just use the Inspect Element feature to pull the xpath.
Hope this helps. Let me know if I can help further.
Edit: Instructions To Change When Data Is Imported
In the toolbar go to the script editor
Now in the scripts, paste the code listed bellow
/**
* Go through all sheets in a spreadsheet, identify and remove all spreadsheet
* import functions, then replace them a while later. This causes a "refresh"
* of the "import" functions. For periodic refresh of these formulas, set this
* function up as a time-based trigger.
*
* Caution: Formula changes made to the spreadsheet by other scripts or users
* during the refresh period COULD BE OVERWRITTEN.
*
* From: https://stackoverflow.com/a/33875957/1677912
*/
function RefreshImports() {
var lock = LockService.getScriptLock();
if (!lock.tryLock(5000)) return; // Wait up to 5s for previous refresh to end.
// At this point, we are holding the lock.
var id = "YOUR-SHEET-ID";
var ss = SpreadsheetApp.openById(id);
var sheets = ss.getSheets();
for (var sheetNum=0; sheetNum<sheets.length; sheetNum++) {
var sheet = sheets[sheetNum];
var dataRange = sheet.getDataRange();
var formulas = dataRange.getFormulas();
var tempFormulas = [];
for (var row=0; row<formulas.length; row++) {
for (col=0; col<formulas[0].length; col++) {
// Blank all formulas containing any "import" function
// See https://regex101.com/r/bE7fJ6/2
var re = /.*[^a-z0-9]import(?:xml|data|feed|html|range)\(.*/gi;
if (formulas[row][col].search(re) !== -1 ) {
tempFormulas.push({row:row+1,
col:col+1,
formula:formulas[row][col]});
sheet.getRange(row+1, col+1).setFormula("");
}
}
}
// After a pause, replace the import functions
Utilities.sleep(5000);
for (var i=0; i<tempFormulas.length; i++) {
var cell = tempFormulas[i];
sheet.getRange( cell.row, cell.col ).setFormula(cell.formula)
}
// Done refresh; release the lock.
lock.releaseLock();
}
}
This snippet of code came from Periodically refresh IMPORTXML() spreadsheet function
Last and definitely the least, replace the "YOUR-SHEET-ID"
NOTE: I have not personally tested this code and I cannot vouch for it. I recommend making a copy and testing it there first.
Hopefully, this solves the issue of your data not being imported as often as you want. If you want to manually get "fresh" data, you can just delete/cut the import function and paste it back in.
try in A2:
=ARRAYFORMULA(IFNA(VLOOKUP(C2:C, PlayerList!A:F, {2, 6}, 0)))
and in C2:
=ARRAYFORMULA(QUERY(REGEXEXTRACT(QUERY(IMPORTDATA(
"https://www67.myfantasyleague.com/2019/export?TYPE=liveScoring&L=64741&APIKEY=&W=14&DETAILS=1&JSON=0?273"),
"where Col1 contains 'player id'", 0),
"(player id=""(\d+)).+?(score=""(\d+.\d+))"),
"select Col2,Col4"))
spreadsheet demo
I made a script in appmaker that takes the date inserted in Google sheet and displayed in appmaker. The date is prior by one day.If I insert in Google sheet 10-08-2018 it gets in appmaker 2018-10-07
I don't know why?!
Here is the code in appmaker:
function getCellValue(spreadsheetId, sheetName, cellRange) {
var range = getRange_(spreadsheetId, sheetName, cellRange);
return JSON.stringify(range.getValue());
}
function createAutoTickets(widget) {
var jsonResultDATE = getCellValue(sheet.sheet_id, getSheets(sheet.sheet_id), 'B' + ii.toString());
var newRecord = app.models.AymanTickets.newRecord();
console.info(jsonResultDATE.substr(1, 10));
newRecord.ticket_date = new Date(jsonResultDATE.substr(1, 10));
app.saveRecords([newRecord]);
}
And the whole script code https://docs.google.com/document/d/1jMTHIzK7nIVR2kgBpmwFdYVitnV-8p8Njm9RT6TGHyE/edit?usp=sharing
I have managed to put a plus minus button in my google sheets document but it only applies for one cell. Since I want to use it to update my stock in the sheet i want one button plus and one minus button that applies on multiple columns.
the code i use right now is this
function plus() {
ss=SpreadsheetApp.getActiveSpreadsheet()
s=ss.getActiveSheet()
var currVal=s.getRange("A1").getValue()
var plusVal= currVal +1
s.getRange("A1") .setValue(plusVal)
}
function minus() {
ss=SpreadsheetApp.getActiveSpreadsheet()
s=ss.getActiveSheet()
var currVal=s.getRange("A1").getValue()
var minusVal= currVal -1
s.getRange("A1") .setValue(minusVal)
}
Is there a way to apply it to let's say column from B to H?
I'm just starting with using google sheets and script editor so I don't understand most of the language en tricks of using scripteditor
i think what you are looking for is a loop to run through every column.
function plus() {
ss=SpreadsheetApp.getActiveSpreadsheet()
s=ss.getActiveSheet()
//for loop
//r refers to the row (number of rows you have and c refers to column
for (var c=1; c<(total number of columns+1); c++){
for (var r=1; r<(total number of rows); r++){
var currVal = s.getRange(r,c).getValue();
var plusVal= currVal +1;
s.getRange(r,c) .setValue(plusVal);
}
}
}
Basically what happens here is that the referenced range changes each time the loop is completed. go from cell A1 to A2 etc etc, and when the last row is reached, it goes to B1 etc
for your reference, this is the script that i used.
function selftest1() {
var app = SpreadsheetApp;
var activeSheet = app.getActiveSpreadsheet().getActiveSheet();
for (var c=4; c<8; c++){
for (var r=2; r<13; r++){
var someCell = activeSheet.getRange(r,c).getValue();
someCell = someCell+1;
activeSheet.getRange(r,c).setValue(someCell);
}
}
}
I'm new to Java scripting and Google Apps Scripts so i am sorry if this has already been answered. I was not able to find what i was looking for over the last few months of working on this project.
I am working on a variant of the scripts here:
Delete row in Google Sheets if certain "word" is found in cell
AND
Google Sheet Script - Find Value in Col and Delete Row
I want to create a button, or menu, that will allow someone to enter specific data, and have each row in the spreadsheet containing that data deleted.
I have a test sheet here that illustrates the data i'm working with, formulas i'm using, and has the beginning of the script attached to it:
https://docs.google.com/spreadsheets/d/1e2ILQYf8MJD3mrmUeFQyET6lOLYEb-4coDTd52QBWtU/edit?usp=sharing
The first 4 sheets are pulling data from the "Form Responses 1" sheet via a formula in cell A:3 in each sheet so the data would only need to be deleted from the "Form Responses 1" sheet to clear it from the rest of the sheets.
I tried working this in but i do not think i am on the right track.
https://developers.google.com/apps-script/guides/dialogs
I also posted this on Google Docs Help Forum 60 days ago, but have not received any responses.
Any help would be greatly appreciated.
There's a few steps. For usability of UI this takes a little longer code. In concise form:
The user activates a dialog and enters a string.
Rows w/ the string are deleted (with error handling and confirmation)
(Hopefully this gets you started and you can tailor it to your needs)
Function that initiates the menu:
function onOpen(){
SpreadsheetApp.getUi()
.createMenu('My Menu')
.addItem('Delete Data', 'deleteFunction')
.addToUi();
}
The main workhorse:
function deleteFunction(){
//declarations
var sheetName = "Form Responses 1";
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(sheetName);
var dataRange = sheet.getDataRange();
var numRows = dataRange.getNumRows();
var values = dataRange.getValues();
var delete_string = getUIstring();//open initial UI, save value
if (delete_string.length < 3) return shortStringError()//UI to protect your document from an accidental entry of a very short string.
//removing the rows (start with i=2, so don't delete header row.)
var rowsDeleted = 0;
for (var i = 2; i <= numRows; i++){
var rowValues = values[i-1].toString();//your sheet has various data types, script can be improved here to allow deleting dates, ect.
if (rowValues.indexOf(delete_string) > -1){
sheet.deleteRow(i - rowsDeleted);//keeps loop and sheet in sync
rowsDeleted++;
}
}
postUIconfirm(rowsDeleted);//Open confirmation UI
}
Isolated UI functions to help make above function more concise:
function getUIstring(){
var ui = SpreadsheetApp.getUi();
var response = ui.prompt("Enter the target data element for deletion")
return response.getResponseText()
}
function postUIconfirm(rowsDeleted){
var ui = SpreadsheetApp.getUi();
ui.alert("Operation complete. There were "+rowsDeleted+" rows deleted.")
}
function shortStringError(){
var ui = SpreadsheetApp.getUi();
ui.alert("The string is too short. Enter a longer string to prevent unexpected deletion")
}
I'll just show a way to delete the cell value if it matches your search criteria. It's up to you to connect it to buttons ,etc.
You'll loop through a Sheet Range. When you find the word match, delete it using clearContent()
function deleteSpecificData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("Sheet1!A1:C4");
var values = range.getValues();
var numArray = [1,2,3,4,5,6,7,8,9];
var deleteItem = "Garen";
Logger.log(range);
for(var i=0; i< values.length; i++){
for(var j=0; j<values[i].length; j++){
if(values[i][j] == deleteItem){
var row = numArray[i];
var col = numArray[j];
var range = sheet.getRange(row,col).clearContent();
}
}
}
}
Before:
After:
Helpful answer, but not exactly what I'm looking for
The above answer gives part of the script I'm looking for. I'd like to add another step (or two) but have no idea how.
Here's what I'm trying to accomplish:
I have a column of numbers, and would like to add numbers from another. The part I can't figure out is how to clear the cells after the numbers have been added to the first column.
Like this:
A <---B
1 <---2
2 <---5
3 <---24
On the click of a button, I'd like the numbers from B to be added to A and then B cleared. Column A would become 3, 7, 27, and the cells in Column B would become blank.
function addRange() {
//replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var tRange = sheet.getRange('A1:A3');
var tValues = tRange.getValues();
var sRange = sheet.getRange('B1:B3');
var sValues = sRange.getValues();
for (var i = 0; i < tValues.length; i++) {
tValues[i][0] += sValues[i][0];
}
tRange.setValues(tValues);
sRange.clearContent();
}
You can also add a button to your spreadsheet menu to execute AdmaL's code example above using this code
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menubutton = [ {name: "Add Range", functionName: "addRange"}]; //"addRange" is the name of the function submitted by AdamL Above //This would also need to be included as a function in your script as the function to call when you click the "Custom" menu button
ss.addMenu("Custom", menubutton);
}