Home > Net >  Transposed content issue
Transposed content issue

Time:09-22

trust you're good. I'm working on a project where I sent data from an apk to a google sheet. The fact is that I sent the whole data to a sheet and I also send a kind of transposed data to other sheets, all with custom IDs.

Example here:

In Test_Sheet: ID Region Place Staff thing1 thing2 thing3 thing4 thing5

In test2 sheet:

ID thing1

ID thing2

ID thing3

ID thing4

ID thing5

All works fine but my issue is that when I have fewer items only, the ID will still be in the test2 sheet like all items are present.

ID thing1

ID thing2

ID thing3

ID

ID

Here is the code:

function doPost(e) {
  var ss = SpreadsheetApp.openById('XXXXXXXXXXXXXXXXXXX');
  var sh = ss.getSheetByName('Test_Sheet');
  var sh2 = ss.getSheetByName('test2')

  //custom ID 
  var scriptProperties = PropertiesService.getScriptProperties();
  var counter = scriptProperties.getProperty('counter') || 0;
  var formatDate = Utilities.formatDate(new Date(), "GMT 8", "ddMMyy");
  var zero = (counter < 10) ? '0' : '';
  var ID = "T"   formatDate   zero   counter  ;
    if (counter > 99) counter = 0;
  scriptProperties.setProperty('counter', counter);

  \\send from apk
  var region = e.parameter.region;
  var place = e.parameter.place;
  var staff = e.parameter.staff;
  var thing1= e.parameter.thing1;
  var thing2= e.parameter.thing2;
  var thing3= e.parameter.thing3;
  var thing4= e.parameter.thing4;
  var thing5= e.parameter.thing5;
  
\\Test_Sheet
  sh.appendRow([ID, region, place, staff, thing1, thing2, thing3, thing4, thing4]);

\\test2
  sh2.appendRow([ID, thing1]);
  sh2.appendRow([ID, thing2]);
  sh2.appendRow([ID, thing3]);
  sh2.appendRow([ID, thing4]);
  sh2.appendRow([ID, thing5]);

}

CodePudding user response:

If the problem is that the new rows are created even though an ID does not exists for these records, then first make sure that the ID does exist before writing to the sheet. So in your code, you would do:

 if( thing1 ) sh2.appendRow([ID, thing1])
 if( thing2 ) sh2.appendRow([ID, thing2]) 
 // … and so on
  • Related