Google Apps Script copying data to wrong sheet - google-sheets-macros

I am using this code which is supposed to copy data from "REQUEST" sheet to "MASTER" sheet but actually the code copies data to "MASTER" sheet as well as some other sheet also in the file.
function onFormSubit () {
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var shtForm = ss.getSheetByName("REQUEST");
var shtAgrt = ss.getSheetByName("MASTER");
var rcrdCount = shtAgrt.getRange(1, 25).getValue();
var chkNum = shtForm.getRange(Math.max(shtForm.getLastRow(),2),1).getValue();
shtAgrt.getRange(rcrdCount+1, 2).setValue(chkNum);
}
I don't want the code to copy data to any other sheet.

I don't understand what happened but error is gone away now. Perhaps it went away after deleting all form submit data or after refreshing google sheet.

Related

How do I print the output of google sheets script back to google sheet?

I am trying to use an api for verifying phone numbers and emails for the database I want to create in google sheets. I coded the following on app scripts of sheet.
function phone(phno) {
var response = UrlFetchApp.fetch("https://neutrinoapi.net/phone-validate?user-id=USERNAME_HERE&api-key=API_KEY_HERE&number="+phno+"&country-code=IN")
// Parse the JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data["valid"]);
Logger.log(data["prefix-network"]);
Logger.log(data["type"]
}
The json returns a bunch of things, valid = true\false {If the number is valid}; prefix-network = service_provider; type = mobile\landline etc.
The idea is that if I call = phone(D2) in cell E2 for example, where D2 stores a valid phone number, I want the validity to be shown in E2, which is a boolean of true\false for if the phone number is valid or not. I would also love to show the next 2 columns with prefix-network and type. But they aren't necessarily essential, just desirable.
The API works fine, and I can see the output in the execution logs in App Scripts, and it's correct with a few test data I tried. However, I cannot find any way to display the result back in the sheet in cell E2. The code has to be generic such that if I drag the code for the cell, it should return the output to the subsequent cells.
I hope the question is clear and the code is sufficient to explain it.
Looking forward to the replies.
Thanks in advance for the help!!
Something like this:
function phone(phno) {
var response = UrlFetchApp.fetch("https://neutrinoapi.net/phone-validate?user-id=USERNAME_HERE&api-key=API_KEY_HERE&number="+phno+"&country-code=IN")
// Parse the JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data["valid"]);
Logger.log(data["prefix-network"]);
Logger.log(data["type"]
const sh = SpreadsheetApp.getActiveSheet();
sh.getRange(1,1,1,3).setValues([[data["valid"],data["prefix-network"],data["type"]]]);
}

Google Sheets - getData() returns an error as not defined

So i am using the following code to copy a query into a spreadsheet. When i run the code it gives me ReferenceError: getData is not defined.
if i try the same code on another google sheets document it runs smoothly and copies everything
function importdataNPS() {
let data = getData("https://popsql.com/share/queries/···/last_run.csv?access_token=...");
let sheet = SpreadsheetApp.openById('·····').getSheetByName('Real_NPS_Q4');
sheet.getRange(2,1,sheet.getLastRow(),data[0].length +1).clearContent();
if ( data.length == 0){ return; }
sheet.getRange(1,1,data.length,data[0].length).setValues(data);
}
What am I doing wrong? is there an authorization for the google sheet?

How to edit cell Values

I'm new to the google spreadsheets API node module. I'm asking on how to edit the cell value of a sheet. I have tries following other internet videos to do it but none of them work. I have a variable called doc that is the spreadsheet and the sheet itself is called SheettoUse.
const doc = new GoogleSpreadsheet('CODE');
await doc.useServiceAccountAuth({
client_email: creds.client_email,
private_key: creds.private_key,
});
await doc.loadInfo(); // loads document properties and worksheets
const TicketASheet = doc.sheetsByTitle['Ticket-A']; // or use doc.sheetsById[id]
let sheet = TicketASheet
await sheet.loadCells('A1:AAA1000');
let cells = sheet.getCell(0,0)
console.log(cells.value)
Thanks,
William
.setValue() is used to update cell value in Apps Script.
To set cell value in npmjs, You can refer to the following sample code:
// update cell value
cells.value = 123.456;
// update cell formula
cells.formula = '=A1';
// update cell format
cells.textFormat = { bold: true };
// update cell note
cells.note = 'This is a note!';
await sheet.saveUpdatedCells(); // saves changes in the cell
Reference:
google-spreadsheet
GoogleSpreadsheetCell

Google Sheets API spreadsheets.values.get returning wrong date value

I'm accessing Google spreadsheet data from node.js as follows:
const sheets = google.sheets({version: 'v4', auth});
const res = await sheets.spreadsheets.values.get({
spreadsheetId,
range
});
const rows = res.data.values;
For reference, I'm using spreadsheets.values.get.
There is one cell with the value 9/24/2019. It's formatted as a date and is definitely not stored as a string. However, the value for that cell from the API call is "9/24/3019".
There are other cells with the same value that are correct. I checked the document version history, and this cell has never had any other value.
Anyone have any ideas what might be causing this and/or how to fix it?

Auto Update specified tab in Google spreadsheet from Web via script

I am currently trying to pull in data from a website into my Google Sheet, however, it will automatically pull the data on whatever tab I am on when the sheet updates.
The script I currently have is below. I have 5 tabs in the workbook and the tab I need updated is labeled 'Update' and is the 3rd tab in the workbook.
The data currently pulls in correctly, however, it will not only update the tab I need updated.
function getData() {
var queryString = Math.random();
var cellFunction = '=IMPORTHTML("http://www.golfchannel.com/tours/pga-tour/2017/us-open/?' + queryString + '","table",2)';
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(cellFunction);
}
Change your code line to this:
SpreadsheetApp.getSheetByName('Update').getRange('A1').setValue(cellFunction);
You can set the sheet by calling the method getSheetByName().
Returns a sheet with the given name. If multiple sheets have the same name, the leftmost one is returned. Returns null if there is no sheet with the given name.
Here is a sample snippet:
function myFunction() {
var sss = SpreadsheetApp.openById('FILEID'); // sss = source spreadsheet
var ss1 = sss.getSheetByName('Sheet1');
var ss2 = sss.getSheetByName('Sheet2');
var source_range = ss1.getRange("A1:G10");
var target_range = ss2.getRange("A1:G1");
source_range.copyTo(target_range);
}
Hope this helps.