Home > Software design >  How can I set a plain text format in the target sheet?
How can I set a plain text format in the target sheet?

Time:07-27

I have a custom Google Scripts code that gets the content from one sheet and copies to another.

The problem is that when pasted, the numbers format are not right.

function cloneGoogleSheet2(ssA, ssB) {
  // source doc
  var sss = SpreadsheetApp.openById('1Cd5nYV7kJ8lD6qaOqMes3Ubphlz1WdlZycL3ynyOeNY');

  // source sheet
  var ss = sss.getSheetByName('ALTERAÇÕES');

  // Get full range of data
  var SRange = ss.getDataRange();

  // get A1 notation identifying the range
  var A1Range = SRange.getA1Notation();

  // get the data values in range
  var SData = SRange.getValues();

  // target spreadsheet
  var tss = SpreadsheetApp.openById('1Cd5nYV7kJ8lD6qaOqMes3Ubphlz1WdlZycL3ynyOeNY');

  // target sheet
  var ts = tss.getSheetByName('FORMULÁRIO');

  // Clear the Google Sheet before copy
  ts.clear({ contentsOnly: true });

  // set the target range to the values of the source data
  ts.getRange(A1Range).setValues(SData);
  }

In the source sheet I have something like 1, 2 and when pasted in the target sheet, this same cell becomes 1/2/2022.

CodePudding user response:

In your script, how about the following modification?

From:

ts.getRange(A1Range).setValues(SData);

To:

ts.getRange(A1Range).setNumberFormats(SRange.getNumberFormats()).setValues(SData);
  • By this modification, the number format is also copied.

Reference:

  • Related