More efficient way to add multiple records - google-app-maker

So this works but it takes 15 seconds for a spreadsheet with 60 items.
function addToModel(name,birth,age){
var newRecord = app.models.ImportData.newRecord();
newRecord['PRESIDENT'] = name;
newRecord['BIRTH_PLACE'] = birth;
newRecord['AGE_ELECTED'] = age;
app.saveRecords([newRecord]);
}
function getSpreadsheet(){
var sh = SpreadsheetApp.openById("zzz");
var ss = sh.getSheetByName("Sheet1");
var data = ss.getDataRange().getValues();
THIS WAS WAY ONE, TAKES 15 SECONDS
for (var i=1; i<data.length;i++)
{
addToModel(data[i][1],data[i][2],data[i][3].toString());
}//for loop
}
but I noticed that the command is saveRecordS not saveRecord and with anything in google apps script, the fewer calls the better, so I tried this but it doesn't work
//SAME SPREADSHEET INFO
var result = [];
for (var i=0; i<data.length;i++)
{
var newRecord = app.models.ImportData.newRecord();
newRecord['PRESIDENT'] = data[i][1];
newRecord['BIRTH_PLACE'] = data[i][2];
newRecord['AGE_ELECTED'] = data[i][3].toString();
result.push(newRecord);
}//for loop
app.saveRecords([result]);
Expected result: new records in my table, much faster than the first version. Actual result: "Cannot read property "key" from undefined" which is triggered from the last line (saveRecords). I tried both app.saveRecords(result) and ([result]), same problem both times.
Note: this example is from an appmaker university tutorial that no longer works because of the changes for appmaker v2.

I think that it's model.newRecord() takes time for each new item created, while time on app.saveRecords() is ignorable.
Could you please confirm that you are EU user? as we EU user are facing same issue (link) due to the server location, if it is the case, please help star that issue and give more info to help Google solve that issue. Thanks.

Related

InDesign - Script to find specific words between angle brackets, and copy those to a list

Sorry in advance if I’m not phrasing this question correctly. I know nothing about InDesign scripting, but this would solve a workflow problem I’m having at my company, so I would appreciate any and all help.
I want to find all strings in an InDesign file that are between angle brackets (i.e. <variable>) and export that out into a list. Ideally this list would be a separate document but if it can just be dumped into a text frame or something that’s fine.
Any ideas on how to do this? Thank you in advance for any and all help.
Here is something simple:
app.findGrepPreferences=NothingEnum.NOTHING; //to reset the Grep search
app.findGrepPreferences.findWhat = '<[^<]+?>'; //the word(s) you are searching
var fnd = app.activeDocument.findGrep (); //execute search
var temp_str = ''; //initialize an empty string
for (var i = 0; i < fnd.length; i++) { //loop through results and store the results
temp_str += fnd[i].contents.toString() + '\r'; // add next found item text to string
}
var new_doc = app.documents.add (); //create a new document
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS; //set measurement units to points
var text_frame = new_doc.pages[0].textFrames.add({geometricBounds:[0,0,100,100]});// create a new text frame on the first page
text_frame.contents = temp_str; //output your text to the text frame in the new document
For more data see here.

How to read, change and replace data with Firebase?

I'm trying to make a gambling website simulation, and I need to have a variable that stores the user's balance. This will change when they place a bet.
The variable is called "userbalance" and it set at 500 on Firebase. The user's bet is successfully stored on firebase. Therefore, I need to restrive this bet value again, and take it away from "userbalance", therefore updating their balance.
function storewager(){
var coinref = firebase.database().ref();
var coinwager = document.getElementById("coininput").value;
var userbalance =
coinref.child("coinbet").set(coinwager);
changeuserbal();
}
function changeuserbal(coinwager, userbalance){
var balref = firebase.database().ref().child('userbalance');
var coinwager = document.getElementById("coininput").value;
var userbalchg = balref - coinwager;
window.alert(balref);
balref.child("userbalance").set(userbalchg);
}
I can't understand why it is working correctly in storewager, but not in changeuserbal.
Here's the firebase database layout:
gambling-website-simulationaddclose
coinbet: "66" <--- this is the user bet, which is changed by storewager()
userbalance: 500 <--- I'm trying to take coinbet away from this
Thank you very much for your help, as when I can do this, I should be able to progress a good amount more.
EDIT
I've now managed to send the variable value, not the name To "userbalance". However, the calculation isn't correct. I believe the issue lies with the original value of "userbalance" not being referenced correctly.
Therefore, "userbalance" is just the wager but minus.
bal - coinwager = userbalchg ---> this is uploaded to "userbalance"
0 - coinwager = userbalchg ---> This is what I think is occuring
Here's the updated piece of my code:
function changeuserbal(coinwager){
var balref = firebase.database().ref().child("userbalance");
balref.once("value")
.then(function(snapshot){
var bal = snapshot.child("userbalance").val();
var coinwager = document.getElementById("coininput").value;
var userbalchg = bal - coinwager;
balref.set(userbalchg);
});
}

Google Sheets: delete rows containing specified data

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:

Game Maker Studio: Top 10 Highscores (Seriously)

I feel really dumb for having to post this, but I've been trying to achieve this for an entire week now and I'm getting nowhere!
I'm trying to create a highscore board. Top 10 scores, saved to an INI file. I have searched every single thing on the entire internet ever. I just do not get it.
So what I have tried is this...
I have a "load_room" setup. When this room loads, it runs this code:
ini_open('score.ini')
ini_write_real("Save","highscore_value(1)",highscore_value(1));
ini_write_string("Save","highscore_name(1)",highscore_name(1));
ini_close();
room_goto(room0);
Then when my character dies:
myName = get_string("Enter your name for the highscore list: ","Player1"); //if they enter nothing, "Player1" will get put on the list
highscore_add(myName,score);
ini_open('score.ini')
value1=ini_write_real("Save","highscore_value(1)",0);
name1=ini_write_string("Save","highscore_name(1)","n/a");
ini_close();
highscore_clear();
highscore_add(myName,score);
score = 0;
game_restart();
I'm not worried about including the code to display the scores as I'm checking the score.ini that the game creates for the real values added.
With this, I seem to be able to save ONE score, and that's all. I need to save 10. Again, I'm sorry for asking the same age-old question, but I'm really in need of help and hoping someone out there can assist!
Thanks so much,
Lee.
Why you use save in load_room instead load?
Why you clear the table after die?
You need to use loop for save each result.
For example, loading:
highscore_clear();
ini_open("score.ini");
for (var i=1; i<=10; i++)
{
var name = ini_read_string("Save", "name" + string(i), "");
var value = ini_read_real("Save", "value" + string(i), 0);
if value != 0
highscore_add(name, value);
}
ini_close();
room_goto(room0);
Die:
myName = get_string("Enter your name for the highscore list: ","Player1");
highscore_add(myName, score);
ini_open("score.ini");
for (var i=1; i<=10; i++)
{
ini_write_string("Save", "name" + string(i), highscore_name(i));
ini_write_string("Save", "value" + string(i), string(highscore_value(i)));
}
ini_close();
score = 0;
game_restart();
And few more things...
score = 0;
You need do it when game starts, so here it is unnecessary.
get_string("Enter your name for the highscore list: ","Player1");
Did you read note in help?
NOTE: THIS FUNCTION IS FOR DEBUG USE ONLY. Should you require this functionality in your final game, please use get_string_async.
I used ini_write_string(..., ..., string(...)); instead ini_write_real() because second case will save something like 1000.000000000, and first case will save just 1000.

Windows phone 8 sqlite async issue

Currently i am working on an app that uses sqlite. I have a scenario in which there are graphs according to days of week. User can check graphs of any day. There may be data available for that day or not.
Issue is for the first time every thing goes well. But then if user taps again on the day which actually have data available, app stop working, or i can say it stuck. it didn't crash or any exception.
private async void getGraphData(int p)
{
var checkCount = p;
Color currentAccentColorHex = (Color)Application.Current.Resources["PhoneAccentColor"];
BabySleepChart.Foreground = new SolidColorBrush(ConvertStringToColor(currentAccentColorHex.ToString()));
Conn = new SQLiteAsyncConnection(DB_NAME);
ObservableCollection<Graph> BabydailySleep = new ObservableCollection<Graph>();
var sundayData = await Conn.QueryAsync<BabySleep>("SELECT * FROM BabySleep WHERE today = ?", Convert.ToDateTime(DateTime.Now.AddDays(-p).Date.ToString("d")));
var count = sundayData.Any() ? sundayData.Count : 0;
if (count == 0)
{
MessageBox.Show("No date for " + DateTime.Now.AddDays(-p).ToString("dddddd"), "Data Not Found", MessageBoxButton.OK);
}
else
{
}
}
In else i display graphs. I am using amcharts. (The control doesn't go after the query for 2nd time.)
Please help . How can i solve this issue.
Regards.

Resources