I'm trying to update my google sheet by inserting a date. I want to have different format for different cells.
Using nodeJS api I have a class Sheet
with is used in another class SpreadSheet
witch contains the spreadsheet info to make batchupdates
import {sheets_v4} from "googleapis";
type DatePattern = "dd-mm-yyyy" | "hh:mm:ss" | "dd-mm-yyyy hh:mm:ss" | string
type DateType = "DATE" | "TIME" | "DATE_TIME"
export class Sheet {
private request: sheets_v4.Schema$Request[] = []
/**
*
* @param columnIndex {number} Index of the column (Starting from 0)
* @param rowIndex {number} Index of the row (Starting from 0)
* @param value {string}
*/
setPercentFormula(columnIndex: number, rowIndex: number, value: string) {
this.request.push({
updateCells: {
fields: 'userEnteredValue,userEnteredFormat',
range: {
sheetId: this.sheetId,
startColumnIndex: columnIndex,
endColumnIndex: columnIndex + 1,
startRowIndex: rowIndex,
endRowIndex: rowIndex + 1,
},
rows: [
{
values: [
{
userEnteredValue: {
formulaValue: value
},
userEnteredFormat: {
numberFormat: {
type: "PERCENT",
pattern: "#0.00%;-#0.00%;0%"
}
}
},
]
},
]
}
})
return this
}
/**
*
* @param columnIndex {number} Index of the column (Starting from 0)
* @param rowIndex {number} Index of the row (Starting from 0)
* @param value {string}
* @param type {DateType}
* @param pattern {DatePattern} Pattern format
*/
setDateWithPatternCell(columnIndex: number, rowIndex: number, value: string, type: DateType, pattern: DatePattern) {
this.request.push({
updateCells: {
fields: 'userEnteredValue,userEnteredFormat',
range: {
sheetId: this.sheetId,
startColumnIndex: columnIndex,
endColumnIndex: columnIndex + 1,
startRowIndex: rowIndex,
endRowIndex: rowIndex + 1,
},
rows: [
{
values: [
{
userEnteredValue: {
stringValue: value,
},
userEnteredFormat: {
numberFormat: {
type: type,
pattern: pattern
}
}
},
]
},
]
}
})
return this
}
}
I have other utility function, for boolean value or formula for example. All of they put the actions in the request array, then this array is use in batchUpdate call.
For example, the use of setPercentFormula
work perfectly well.
The result in my sheet is a cell with a starting coma and then the date as string given in the value parameter of my function.
It looks like my formatting isn't accounted.
As it is a number format, I also tried to use userEnteredValue: { numberValue: value }
and give it a timestamp as input but resulting in setting the number in the cell and no date.
I also tried to make to separate call by putting two value in my request array. One setting the string and another one setting the formatting. with the same result.
I would like as a result to have my sheet with my date value as if I manually got in the file and clicked Format > Number > Date
for example. Without the starting coma and a value recognized as a date.
Here are some input I tried :
Sheet.setDateWithPatternCell(0, 0, '2022-08-10', 'DATE', 'yyyy-mm-dd')
Sheet.setDateWithPatternCell(0, 0, Date.now(), 'DATE', 'yyyy-mm-dd')
Sheet.setDateWithPatternCell(0, 0, Date.now(), 'DATE_TIME', 'yyyy-mm-dd hh:mm:ss')
I expect the output in my google sheet to be the date with the format is specified.
Not the string (as I see in my sheet it's starting with a '
in the value)