How to edit cell Values - google-sheets-api

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

Related

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?

Google Apps Script copying data to wrong sheet

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.

Using checkboxes to trigger various scripts, then clear

I have a Google Sheet set up with two buttons on the Form sheet, which are attached to two different scripts. They work perfectly on PC, but unfortunately, custom buttons still do not appear to work on the Google Sheets app for tablets. I was able to incorporate a workaround via a dropdown box, but that is still a bit finicky, so I'm wondering whether I could just switch both PC and tablet users to checkboxes instead.
If the checkbox in cell G3 is checked, the AUTOFILL script should run and the checkbox should be cleared; subsequently, if the checkbox in cell G5 is checked, the UPDATE script should run and its checkbox be cleared.
What would be the best way of doing this, now that checkboxes are a thing in Google Sheets?
Here is the code I am currently using, working for both the buttons and the dropdown:
function onEdit(e) {
if (e.range.getA1Notation() == 'D3') {
if (/^\w+$/.test(e.value)) {
eval(e.value)();
e.range.clearContent();
}
}
}
function AUTOFILL() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form');
var valueOfData = sheet1.getRange(sheet1.getLastRow(), 1).getValue();
sheet2.getRange('B3').setValue(valueOfData + 1);
}
function UPDATE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form");
var dataSheet = ss.getSheetByName("Data");
var values = formSS.getRange("B3:B6").getValues().reduce(function(a, b) {
return a.concat(b)
});
var partNum = values[0];
var row;
dataSheet.getDataRange().getValues().forEach(function(r, i) {
if (r[0] === partNum) {
row = i + 1
}
})
row = row ? row : dataSheet.getLastRow() + 1;
var data = dataSheet.getRange(row, 1, 1, 4).getValues()[0].map(function (el, ind){
return el = values[ind] ? values[ind] : el;
})
var now = [new Date()];
var newData = data.concat(now)
dataSheet.getRange(row, 1, 1, 5).setValues([newData]);
formSS.getRange("B3:B6").clearContent()
}
A you correctly said, running scripts on button clicks does not appear to work on the Android mobile app. This is an issue that has already been reported (see this and this). A common workaround used to be using Android add-ons but they are now deprecated.
In order to make your script run using checkbox, one thing you can do is to modify your onEdit function. After the following modifications, it will check whether any of the checkboxes is enabled, run the appropiate function based on that, and then disable it again. You can see the updated onEdit function below:
function onEdit(e) {
var isAutofill = SpreadsheetApp.getActiveSheet().getRange("G3").getValue();
var isUpdate = SpreadsheetApp.getActiveSheet().getRange("G5").getValue();
if (isAutofill && isUpdate) {
Browser.msgBox("You cannot autofill and update at once!");
SpreadsheetApp.getActiveSheet().getRange("G3").setValue(false);
SpreadsheetApp.getActiveSheet().getRange("G5").setValue(false);
} else if (isAutofill) {
AUTOFILL();
SpreadsheetApp.getActiveSheet().getRange("G3").setValue(false);
} else if (isUpdate) {
UPDATE();
SpreadsheetApp.getActiveSheet().getRange("G5").setValue(false);
}
if (e.range.getA1Notation() == 'D3') {
if (/^\w+$/.test(e.value)) {
eval(e.value)();
e.range.clearContent();
}
}
}

How to get list of sheets id(s) from specific SpreadSheet with Google Sheets API?

i can put data in the cell,
i can CopyTo to make new sheet from existing sheet.
but when i want to delete several sheets, i need list of sheet id(s)
but i don't know how to get the list of sheet id(s) from speadsheet?
does somebody know how to do it?
The following function will display all sheet names and id's for the active spreadsheet.
function getSheetId()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var allSheets=ss.getSheets();
var ids=[];
for(var i=0;i<allSheets.length;i++)
{
ids[allSheets[i].getName()]=allSheets[i].getSheetId();
}
var s='<table width="100%">';
for(key in ids)
{
s+=Utilities.formatString('<tr><td><strong>Sheet Name</strong></td><td>%s</td><td><strong>Sheet Id</strong></td><td>%s</td></tr>',key,ids[key]);
}
s+='</table>'
var userInterface=HtmlService.createHtmlOutput(s).setWidth(800).setHeight(450);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Sheet Keys for Spreadsheet: ' + ss.getName())
}

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.